我正在嘗試用MongoDB,Angular,NODE.js和Express創建一個簡單的留言板。HTTP GET沒有被解僱
出於某種原因,我第一次調用getMessages()一切都很順利。 但是,當我在postMessage()之後調用getMessages()時,沒有發送GET請求。
這是我的路線:
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');
})
,這是我的角度控制器:
(function() {
'use strict';
angular
.module('app')
.controller('MainController', mainController);
function mainController(navigationFactory, $http, $timeout, apiFactory, $scope) {
var that = this; // jshint ignore: line
function init() {
that.getMessages();
}
that.postMessage = function() {
apiFactory.postMessage(that.message).then(function() {
console.log('Posted from controller'); //gets logged
that.getMessages();
});
//that.getMessages(); <-- also tried this
}
that.getMessages = function() {
console.log('Getting from 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();
}
})();
最後我用一個工廠張貼訊息:
factory.postMessage = function(message) {
return $http.post('http://localhost:5000/api/message', {msg: message});
}
我已經開了一個類似的問題,但由於這是一個不同的問題,我想我會再問一次。
爲什麼沒有HTTP GET請求,即使在我的postMessage()函數中明確地調用了getMessages()。我可以看到getMessages()在console.log()的幫助下被調用,但它似乎在發送任何請求之前返回。
您是否收到任何控制檯錯誤? –
完全沒有錯誤,我可以在網絡選項卡中看到我的POST請求,狀態爲200,但GET請求未被髮送。 (僅在網站首次推出時) –
請您嘗試製作小提琴嗎? –