node.js - TypeError: login.findOne(...).toArray 不是函数
问题描述
我有这个功能,基本上是从前端表单获取用户输入的用户名和密码,然后在mongodb中检查:
app.post('/login', (req, res, next) => {
var username = req.body.username;
var password = req.body.password;
//connecting to the mongo client
client.connect().then (() => {
//defining database name and collection
const database = client.db("myFirstDatabase");
const login = database.collection("login");
//connecting to the mongo client
MongoClient.connect(uri, function(err, db) {
if (err) throw err;
//finding all documents inside array
login.findOne({"username": username}).toArray(function(err, result) {
if (err) throw err;
result.forEach(results =>
bcrypt.compare(password, results.password, function(err, result) {
if (result === true) {
req.session.loggedin = true
next()
} else {
res.redirect('/login')
}
})
);
db.close();
});
});
})
})
但是,它给了我这个错误:
TypeError: login.findOne(...).toArray is not a function
我以前从未遇到过这个错误。我该如何解决?
解决方案
试试这个方法
login.findOne({"username": username}, function(err,user)
{ console.log(user); });
推荐阅读
- c# - 如何在 MVC 控制器中获取当前的 url 协议?
- c++ - 我不确定这段代码的行为。有人可以向我解释吗?
- r - 如何通过从两个不同的列中获取 x 轴值来绘制?
- c# - UWP 和 BitmapDecoder 问题 - 感觉像是线程问题
- python - concurrent.futures 库中 ProcessPoolExecutor 中的奇怪行为
- python-3.x - 这个继承的我的python tkinter网格在哪里?
- regex - 用正则表达式替换url中的字符
- c# - 当前上下文 C# 中不存在名称 VARname
- c - 在条件下生成随机数
- javascript - 如何将其转换为 html 可读代码?