简体   繁体   English

尝试在 MongoDB 中使用 updateOne(),但它没有更新文档或给我一个错误?

[英]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.

相关问题 MongoDB UpdateOne 未更新,尽管它给出了成功消息 - MongoDB UpdateOne Not Updating, Despite It Giving a Success Message 更新 MongoDB 中的文档时出错 - Error while updating a document in MongoDB 如何使用 NodeJS 在 Mongodb 中使用 updateOne - How to use updateOne in Mongodb using NodeJS 试图将输入值从表单显示到表中,但它给了我 405 错误 - Trying to display input values from a form into table but it's giving me 405 error MongoDB updateOne 获取最旧的文档我想要最新的 - MongoDB updateOne gets the oldest document I want the newest 尝试将此html发送到localstorage,以便我可以在新的购物车页面中使用它,但它一直给我一个错误 - Trying to send this html into localstorage so I can use in a new cart page but it keep giving me an error document.getElementById('inner')。innerHTML =“ message”; 给我IE错误 - document.getElementById('inner').innerHTML=“message”; is giving me an error with IE 在 MongoDB 中更新文档的问题 - Issue with updating a document in MongoDB 在 mongodb 集合上使用 updateOne() function 令人困惑的错误消息 - Confusing error message using updateOne() function on a mongodb collection Mongoose - 将嵌入文档添加到现有文档时,Model.updateOne 对我不起作用 - Mongoose - Model.updateOne isn't working for me when adding an embeded document to an existing document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM