【发布时间】:2016-01-29 20:08:56
【问题描述】:
所以我正在尝试创建一个注册路由来检查用户是否首先存在,并且我在需要返回 true 或 的单独函数中调用了数据库>false 完成后。问题是我对回调不是很熟悉,而且我搜索的所有东西似乎都不起作用的整个异步事情一直给我。
TypeError: callback is not a function
这是我的代码,任何帮助或指导将不胜感激。
function pullUserFromDatabase(username, callback) {
console.log(username); //for debug
mongodb.connect(url, function(err, db) {
if(err) {
console.log("didn't get far" + err) //for debug
}
var collection = db.collection(username);
collection.findOne({username}, function(err, item) {
if(err) {
console.log("nope it broke" + err) //for debug
} else {
console.log("it worked" + JSON.stringify(item)) //for debug
callback(true);
}
});
});
}
app.post("/signup", function(req, res) {
var username = req.headers["username"],
password = req.headers["password"],
randomSalt = crypto.randomBytes(32).toString("hex"),
passwordHashOutput = crypto.createHash('sha256').update(password + randomSalt).digest("hex");
if(!username || !password) {
res.send("Username or password not provided.")
} else if(pullUserFromDatabase(username)) {
res.send("User exist.")
}
});
【问题讨论】:
标签: javascript node.js mongodb asynchronous callback