我们如何确定 mongodb 中是不是已经存在用户名和电子邮件地址..?

Posted

技术标签:

【中文标题】我们如何确定 mongodb 中是不是已经存在用户名和电子邮件地址..?【英文标题】:How can we find out if a username and email address already exists in mongodb..?我们如何确定 mongodb 中是否已经存在用户名和电子邮件地址..? 【发布时间】:2021-08-04 22:27:47 【问题描述】:

我的代码是

User.find($or:['email':req.body.email,'username':req.body.username])
    .exec()
    .then(user=>
        if(user.length >=1)
            
                return res.status(409).json(
                    message: 'Email and Username exixts'
                ) 
            
        
    else

        bcrypt.hash(req.body.password,10,(err,hash)=>
            if(err)
                return res.status(500).json(
                    error:err
                )
             else
                
                const user = new User(
                    _id: new mongoose.Types.ObjectId(),
                    email: req.body.email,
                    username: req.body.username,
                    password: hash
                )
                user
                .save()
                .then(result=>
                    console.log(result)
                    res.status(201).json(
                        message: 'User created'
                    )
                )
                .catch(err=>
                    console.log(err)
                    res.status(500).json(
                        error:err
                    )
                )
            
        )
    
)

这里我的代码有效,但如果电子邮件和用户名没有退出,它不会发送有效的错误消息。在这里我只能发送电子邮件不退出消息如何在这里发送用户名不存在消息。我被困在这里有人帮助我

【问题讨论】:

【参考方案1】:

我得到了解决方案..我将代码更改如下,现在它可以工作了

User.find($or:['email':req.body.email,'username':req.body.username])
.exec()
.then(user=>
    if(user.length >=1)
            let errors =
            if (user[0].email === req.body.email)
                errors.email = "Email already exists"
             else if(user[0].username === req.body.username) 
                errors.username = "User Name already exists"
            
            return res.status(409).json(errors)
        
    
    else

        bcrypt.hash(req.body.password,10,(err,hash)=>
            if(err)
                return res.status(500).json(
                    error:err
                )
             else
                
                const user = new User(
                    _id: new mongoose.Types.ObjectId(),
                    email: req.body.email,
                    username: req.body.username,
                    password: hash
                )
                user
                .save()
                .then(result=>
                    console.log(result)
                    res.status(201).json(
                        message: 'User created'
                    )
                )
                .catch(err=>
                    console.log(err)
                    res.status(500).json(
                        error:err
                    )
                )
            
        )
    
)

【讨论】:

以上是关于我们如何确定 mongodb 中是不是已经存在用户名和电子邮件地址..?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Smack 确定 MUC 是不是存在?

我如何确定 mongodb 是不是返回空结果

如何在 mongodb/mongoose 的嵌套数组中检查是不是存在并更新对象?

如何检查表中是不是已经存在时间限制?

iOS:我如何确定用户是不是仍有资格免费试用应用内购买订阅?

如何确定帖子是不是被 Mongoose 的用户点赞