简体   繁体   中英

How to update subdocument mongoose with NodeJS

console.log("check")
 
Email.findOneAndUpdate(
    { _id: req.params.idUser, "sendedEmail.emailId": req.params.idEmail},
    { 
        $set: {
            "sendedEmail.$.isSeen": true
        }
    },
    function(error) {
        if (error) {
            console.log(error);
        }
    }
);

var emailList = await Email.find()

res.send(emailList)

it work fine with localhost, but in vps it doesn't change anything

sorry for my bad english

modify this line: var Email = mongoose.model('Email', emailSchema). Mongoose will make a collection called: 'emails'

 const mongoose = require('mongoose'); var emailSchema = new mongoose.Schema({ subscriberEmail: String, sendedEmail: Array, }, { versionKey: false } ) var Email = mongoose.model('Email', emailSchema); module.exports = Email;
Try this syntax with findById() method:

 updateEmail: (req, res) => { let Id = req.params.id; Email.findById(Id, (err, res) =>{ if(err) return res.status(500).send({message: 'Error to find'}); }); let update = req.body; Email.findByIdAndUpdate(Id, update, {new: true}, (err, EmailUpdated) => { if(err) return res.status(500).send({message: 'Error while updating'}); if(!EmailUpdated) return res.status(404).send({message: 'Email doesnt exist'}); return res.status(200).send({email: EmailUpdated}); }); },

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