【发布时间】: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 操作上。