简体   繁体   中英

MongoDB (mongoose) in Nodejs - save acts strange

I use this in router in nodejs to save a value posted on html.

router.post('/', function(req,res,next) {
    User.findOne({ '_id': req.user._id }, function (err, doc){
        console.log(doc);
        if(err) {  console.log(err);
         res.json({success: false, msg: 'Error: database error.'}); };

            doc.profile.name = req.body.value;
            doc.save();
        console.log(doc);

        return res.json({success: true});
    });

});

when I output the doc after I save it works perfectly but after I refresh or look at the database itself it did not modify at all.

It used to work. What I saw and I don't get why, is if I use doc.save() , then again doc.save() two times it works and updates the data eventually.

Can someone explain what may happen?

So for someone who can not debug the problem , you can use .

.save(function (err,obj){
 console.log(err);
});

for me it was a problem with the structure of my db because I've added some enums and I had empty field so that caused the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM