嘿傢伙我有問題,我不知道它有什麼問題,我試圖從數據庫獲取數據在http調用並將其返回到前端。這是我迄今爲止無法從http.get調用數據庫獲取數據
app.get('/contentHandler/post/frontPage', contentHandler.displayMainPage);
displayMainPage
this.displayMainPage = function(req, res, next) {
"use strict";
posts.getPosts(10, function(err, result) { //the 10 is limit to 10 post
"use strict";
if(err) return next(err);
res.send(200,result); // send it to front end
});
}
getPosts
this.getPosts = function(num, callback) {
"use strict";
posts.find().sort('date', -1).limit(num).toArray(function(err, items) {
"use strict";
if (err) return callback(err, null);
console.log("Found " + items.length + " posts");
callback(err, items);
});
}
前端(角JS控制器)
function IndexCtrl($scope, $http) {
$http.get('/contentHandler/post/frontPage').
success(function(data) {
alert(data); // alert nothing/blank
}).error(function(err) {
});
}
即使我刪除逗號我仍然得到相同的結果,這是什麼也沒有 –