【问题标题】:UnhandledPromiseRejectionWarning: MongoError: w has to be a number or a string at ConnectionUnhandledPromiseRejectionWarning: MongoError: w has to be a number or string at Connection
【发布时间】:2020-11-09 02:15:53
【问题描述】:

任何人都知道我为什么会收到此错误:“UnhandledPromiseRejectionWarning: MongoError: w has to be a number or a string at Connection.” ?我在运行下面的代码时遇到了这个错误。它的目的是检查用户是否在 mongodb 数据库中,如果不是,它会通过用户电子邮件和哈希密码创建一个新用户。

我不知道是否有任何关系,代码似乎运行良好,但是当我昨天将我的 mac 更新到 catalina OS 时,我开始遇到这个问题。

routerAuth.post('/signup', (req, res, next) => {
    const result = Joi.validate(req.body, schema)
    if (result.error === null) {
        Profile.findOne({
            email: req.body.email
        }).then(profile => {
            if (profile) {
                const error = new Error(
                    'The email is already in use. Please choose another one'
                )
                res.status(409)
                next(error)
            } else {
                bcrypt.hash(req.body.password.trim(), 12).then(hashedpassword => {
                    let newProfile = new Profile({
                        first: req.body.first,
                        last: req.body.last,
                        password: hashedpassword,
                        email: req.body.email
                    })

                    Profile.insertMany(newProfile).then(profile => {
                        res.json(profile)
                    })
                })
            }
        })
    }
})

【问题讨论】:

  • 尝试将 .catch 块添加到 Profile.findOne 承诺中。
  • 感谢您的帮助,我试了一下,但仍然遇到同样的错误。
  • 检查 Promise 是被解决还是被拒绝。如果被拒绝,请在此处粘贴详细的错误日志。
  • 我尝试按照您的建议进行操作。这是我得到的结果 "error": { "operationTime": "6754414467615817729", "ok": 0, "errmsg": "w has to be a number or a string", "code": 9, "codeName" :“FailedToParse”,“$clusterTime”:{“clusterTime”:“6754414467615817729”,“签名”:{“哈希”:“38e2Mt0iCFbOolZqkqpz+o0LxPE=”,“keyId”:“6737882210780577793”}},“名称”:“ MongoError" }
  • 它是否仍在产生 UnhandledPromiseRejectionWarning?我需要知道问题出在 findOne 操作还是 insertMany 操作上。

标签: mongodb express


【解决方案1】:

我不确定这个错误来自哪里(我发现了一个 2016 年的拉取请求,据称它修复了这个问题),但是当我更改 url 参数顺序时,我再也没有得到这个。

所以不要这样:

/<database>?retryWrites=true&w=majority

写这个:

/<database>?w=majority&retryWrites=true

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。我不知道为什么,但就我而言,当我从 MongoDB 连接字符串中删除“&w=majority”部分时,它起作用了。

    【讨论】:

      猜你喜欢
      • 2016-11-13
      • 2019-12-29
      • 2022-12-26
      • 2022-11-11
      • 2017-03-09
      • 2022-12-02
      • 1970-01-01
      • 2021-06-25
      • 2018-06-07
      相关资源
      最近更新 更多