【发布时间】:2021-02-20 16:34:52
【问题描述】:
const {email, username} = req.body
const userExistsbyEmail = User.findOne({
$and: [{ _id: { $ne: req.params.id } }, { email }],
})
const userExistsbyUsername = User.findOne({
$and: [{ _id: { $ne: req.params.id } }, { username }],
})
if (userExistsbyEmail || userExistsbyUsername) {
res.status(400)
throw new Error('User already exists')
}
我想在更新用户配置文件时获取用户名和电子邮件的详细信息,如果有则显示抛出错误?
谢谢
【问题讨论】:
-
不知道为什么要按 id 和 2 个单独的查询进行搜索。我会选择
User.findOne({ $and: [{ username }, { email }] });