angularjs - HTTP GET not being fired -
i'm trying create simple messageboard mongodb, angular, node.js , express.
for reason first time call getmessages() goes fine. when call getmessages() after postmessage(), no request being send.
these routes:
app.get('/api/message', function(req, res) { message.find({}).exec(function(err, result) { res.send(result); }); }); app.post('/api/message', function(req, res) { console.log(req.body); var message = new message(req.body); message.save(); res.status(200).send('message added'); })
and angular controller:
(function() { 'use strict'; angular .module('app') .controller('maincontroller', maincontroller); function maincontroller(navigationfactory, $http, $timeout, apifactory, $scope) { var = this; // jshint ignore: line function init() { that.getmessages(); } that.postmessage = function() { apifactory.postmessage(that.message).then(function() { console.log('posted controller'); //gets logged that.getmessages(); }); //that.getmessages(); <-- tried } that.getmessages = function() { console.log('getting controller'); //gets logged $http.get('http://localhost:5000/api/message').then(function(result) { console.log(result.data); //logs old result, without new message that.messages = result.data; console.log('set data'); //gets logged }); } init(); } })();
and use factory post message:
factory.postmessage = function(message) { return $http.post('http://localhost:5000/api/message', {msg: message}); }
i opened similar question, since has turned out different problem thought i'll ask again.
why there no http request, though getmessages() being called in postmessage() function. can see getmessages() being called of console.log(), seems return before request being sent out.
i think, issue @ server side promise, try using this.
app.get('/api/message', function(req, res) { return message.find({}).exec(function(err, result) { if(err) { return res.status(500).send(err); } return res.status(200).json(result); }); });
Comments
Post a Comment