我正在尝试使用 findOne 的 name 属性从我的集合中获取用户,但它返回 undefined 而我的用户在数据库中。
这是我的架构:
var schema = new mongoose.Schema({
name: {
type: String,
required: [true, "Please enter a name"],
maxlength: 32,
},
password: {
type: String,
required: true,
minlength: [6, "Password must be at least 6 characters"],
},
email: {
type: String,
required: true,
unique: true,
},
isAdmin: {
type: Boolean,
default: false,
},
});
const Userdb = mongoose.model("userdb", schema);
module.exports = Userdb;
这就是我试图让我的用户:
const name = req.body.name;
const password = req.body.password;
const user = Userdb.findOne({name: name});
// console.log(user.name, user.password);
if (user.name === undefined)
return res.status(400).send({ message: "User not found" });
const dbpassword = user.password;
我的 console.log 返回 undefined
在数据库中:
我的邮递员发帖请求:
检查您的正文请求,并确保您在这种情况下在 db 集合中有数据。如果收集的数据与查询 mongoose 不匹配,则返回未定义的数据。
你应该创建异步函数并使用等待获取数据然后记录
const user = await Userdb.findOne({name: name});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句