我有一个 var 函数来检查数据库中的用户名,但我不知道如何在 Node.js 中调用它。
这是我到目前为止所得到的:
// Get user_name from post.
app.post("/api/exercise/new-user", function(req, res) {
let user_name = req.body.username; // Gets the URL from the body of the page.
//Call findPeopleByName here.
});
// Check the database if user exists.
var findPeopleByName = function(userName, done) {
User.find({username:userName}, (err, data) => {
if (err) {
done(err);
}
done(null, data);
});
};
最佳答案
首先,你的函数findPeopleByName
应该在调用之前定义。其次,调用这个函数,并不真正知道你想要什么回调done
是,findPeopleByName(user_name, done)
如果替换 done
应该可以工作通过您想要在 findPeopleByName
中调用的函数.
关于javascript - 如何在 Node.js 中调用 var 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53797972/