Updating a nested array with Express and Mongoose

I have an Express app with Mongoose. I have a model

var AttendeeSchema   = new Schema({
    name: String,
    registered: Boolean
});

var EventSchema   = new Schema({
    name: String,
    description: String,
    attendees : [AttendeeSchema],
    created: { type: Date, default: Date.now },
    updated: { type: Date, default: Date.now }
});

      

Code for creating an event:

router.route('/:event_id')
    .get(function(req, res) {
        Event.findById(req.params.event_id, function(err, event) {
            if (err)
                res.send(err);
            res.json(event);
        });
    })

    .put(function(req, res) {
        Event.findById(req.params.event_id, function(err, event) {

            if (err)
                res.send(err);

            event.name = req.body.name;
            event.description = req.body.description; 
            event.attendees: req.body.attendees;

            event.save(function(err) {
                if (err)
                    res.send(err);

                res.json({ message: 'Event updated successfully!' });
            });

        });
    })

      

Code to update the event:

router.route('/:event_id')    
    .put(function(req, res) {
        Event.findById(req.params.event_id, function(err, event) {

            if (err)
                res.send(err);

            event.name = req.body.name;
            event.description = req.body.description; 
            event.attendees: req.body.attendees;

            event.save(function(err) {
                if (err)
                    res.send(err);

                res.json({ message: 'Event updated successfully!' });
            });

        });
    })

      

The problem is the events are being thrown successfully, however when I try to update the event, it only updates the event name and description, but not changes to the member name or registered status. I also noticed that the version is not updated from "__v0" to "__v1"

Are there any hints as to why I can't update specific visitor information with the above code?

+3


source to share


1 answer


Looks like you need =

where you have it :

;)



event.attendees: req.body.attendees;

      

+2


source







All Articles