Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
Create object in db AND turn its objectId into a cookie

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!