Moh*_*sam 6 mongoose mongodb node.js
我正在使用 MERN 堆栈,我遇到一个问题,即 mongoose findOne 返回找到的第一个对象,即使条件不为真这是我的用户模型
{ local:{
username,
email,
password
},
google: {
googleId,
email,
name
}
}
Run Code Online (Sandbox Code Playgroud)
我有这个模式静态来查找电子邮件是否存在
userSchema.statics.findByEmail = async (email) => {
const localUser = await User.findOne({ "local.email": email });
const googleUser = await User.findOne({ "goolge.email": email });
return { localUser, googleUser };
};
Run Code Online (Sandbox Code Playgroud)
在我的 api 中,在添加用户之前,我使用 findByEmail 在数据库中搜索它
router.post("/register", async (req, res) => {
const user = await User.findByEmail(req.user.email);
})
Run Code Online (Sandbox Code Playgroud)
如果我的数据库中没有文档,API 可以正常工作,但如果至少有一个文档,它总是返回我的数据库中的第一个文档,即使它 req.user.email
不在我的数据库中
| 归档时间: |
|
| 查看次数: |
2917 次 |
| 最近记录: |