【发布时间】:2016-10-20 14:57:51
【问题描述】:
我正在做一个 nodeJS 应用程序,其中有一个回调函数将值保存到数据库(MongoDB)。回调函数如下:
exports.add = function(student, cb) {
var collection = db.get().collection('students');
collection.insert(student, function(err) {
if (err) {
throw err;
}
console.log("Record added");
});
}
上面的回调函数是从另一个函数调用的,如下所示:
router.post("/add", function(req,res){
var student = req.body;
Students.add(student, function(err) {
if (err) {
throw err;
}
var respOut = JSON.stringify({id:student.id});
console.log("respOut");
res.send(respOut);
});
});
回调函数是从上面的粗体(Students.add)部分调用的。目前我能够将数据保存到数据库中。但在控制台上没有得到任何输出(给定控制台输出为 -> console.log("respOut"); 或 UI 中的任何响应(来自上面的代码)。我的回调函数有什么问题吗?或者我遗漏了什么?
【问题讨论】:
-
您没有在exports.add 中调用cb(err),它应该在您的console.log("Record added") 之后或之前
标签: javascript jquery angularjs node.js