[英]Trying to use updateOne() in MongoDB, but it's not updating the document or giving me an error?
I'm trying to add a value obtained from a calculator to a user's document in the database, however it's not showing any error or updating the document, the page just loads forever and I'm not sure why.我正在尝试将从计算器获得的值添加到数据库中的用户文档中,但是它没有显示任何错误或更新文档,页面只是永远加载,我不知道为什么。 Below is the code where I'm trying to update:
下面是我要更新的代码:
router.post('/tdeecalculator', function(req, res){
User.updateOne({email: "testtesttest@test.com"}, {$set: {tdeenumber: req.body.tdee}})
});
And below is my user schema:以下是我的用户架构:
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
tdeenumber: {
type: Number,
required: false
},
ormnumber: {
type: Number,
required: false
}
})
const User = mongoose.model('User', UserSchema);
module.exports = User;
The document in MongoDB shows as this: MongoDB中的文档显示如下:
_id: 60c217c26c83903ee454c2c5
name: "testtesttest"
email: "testtesttest@test.com"
password: "$2a$10$zQ4Jk3KKCjjNTyY0Z48/X.JkPO0J5lfV6j4gqTR1sLmqxKqvSq8mW"
__v: 0
Could it be because the tdeenumber is not shown in the actual document?可能是因为实际文档中没有显示 tdeenumber 吗? Any help would be much appreciated
任何帮助将非常感激
You should've used async/await
before db query also you're not returning a response to the call you receive.您应该在 db 查询之前使用
async/await
并且您没有返回对您收到的呼叫的响应。
Convert your controller like this and see the error correctly.像这样转换您的 controller 并正确查看错误。
// Used async before the function to use await before the db query
router.post('/tdeecalculator', async (req, res) => {
//Wrapped your operation with try/catch block to catch error message and show it in console and response
try {
await User.updateOne({ email: "testtesttest@test.com" }, { $set: { tdeenumber: req.body.tdee } })
res.send({ success: 1 });
return;
} catch (error) {
console.log(error.message);
res.send({
success: 0,
error: error.message
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.