我使用Node.js查询MongoDB数据库。但是,使用该函数时未能发送数据,我不知道出了什么问题。
var findIco = function(db, callback) {
var cursor =db.collection('footIco').find();
cursor.each(function(err, doc) {
// console.log(err);
if (doc != null) {
console.log(doc); <------ DISPLAY THE DATA IN THE CONSOLE
} else {
callback();
}
});
};
app.get('/icons', function(req, res){
//calling the function
MongoClient.connect(url, function(err, db) {
if(err) {
console.log(err);
return res.status(500).send(err);
}
findIco(db, function(icons) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
console.log(icons);<--------------- IS UNDEFINED
res.json(icons);
db.close();
return;
});
});
});
app.listen(8080);
我做错了什么?
app.controller('footIconCtrl', ['$scope', '$http', function($scope, $http) {});
。 - peteblocalhost
。 - peteb