I'm trying to create an object in a db then store its objectId as a cookie when the guest adds a phone number to my event model.
function
module.exports.addGuest = async (req, res) => {
const event = await Event.findById(req.params.id);
const guest = req.body.guest;
event.guests.push(guest);
await event.save();
console.log(guest, req.body.guest._id );
res.cookie('guest_id', `${guest._id}`);
res.cookie('event_id', `${event._id}`);
res.redirect(`/events/${event._id}`);
}
the console.log above yields { phone: '4444444444' } undefined
event model
const eventSchema = new Schema({
event_name: String,
},
artist: {
type: Schema.Types.ObjectId,
ref: 'Artist'
},
guests: [
{
phone: Number,
attended: String
}
],
created: {
type: Date, // Captures both date and time
default: Date.now
},
event_start: {
type: Date,
required: [true, 'Date & time of event start required']
},
event_end: {
type: Date,
required: [true, 'Date & time of event end required']
},
}, opts);
How do I get the object ID into my controller function so I can store it as a cookie?