1

I have this code:

<a class="btn-primary btn" href="/employer/booth/<%= user._id.toString() %>"
        <% console.log(typeof user._id, user._id) %>
   target="_blank">
    Preview your booth
</a>

The result of this console.log is this: object 5bc9d28270e5375dfbfe17fd

Why? Why is a generic _id created with passport treated as an object when it's clearly a string?

I tried doing .toString(), but this caused another error in my router. Here's the code:

router.get('/booth/:emp_id', async (req, res, next) => {
    const mongoose = require('mongoose');
    // For reports
    const {emp_id} = req.params;
    console.log('here', emp_id);
    console.log(typeof  emp_id);
    console.log(JSON.stringify(emp_id));
    const eventId = await GeneralInfo.findOne().then(r => r.activeEventId).catch(e => console.log(e));
    const event = await Event.findById(eventId).catch(e => console.log(e));
    Event.updateOne({'attendants.employers.id': mongoose.Types.ObjectId(emp_id)}, {$addToSet: {"attendants.employers.$.boothVisits": req.user._id}}, {$upsert: true}).catch(e => console.log(e));
    var postings = await Posting.find({
        'creatorId': emp_id,
        'visible': true,
        'eventId': eventId,
    }).catch(e => console.log(e));
    const acc = await Account.findById(emp_id);
    // const logo = await functions.downloadFile(req, res);
    const logo = await functions.downloadFile(acc.employer.logo);
    console.log(logo);
    res.render('users/employer/booth', {
        title: 'Employer Booth',
        user: req.user,
        postings: postings,
        employer: acc,
        event: event,
        logo: logo,
    });
});

If I do this, so pass a string I get this:

(node:15508) UnhandledPromiseRejectionWarning: Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
at new ObjectID (/home/alex/Documents/Projects/ontario-job-portal/node_modules/mongodb-core/node_modules/bson/lib/bson/objectid.js:57:11)
at ObjectID [as ObjectId] (/home/alex/Documents/Projects/ontario-job-portal/node_modules/mongodb-core/node_modules/bson/lib/bson/objectid.js:38:43)
at router.get (/home/alex/Documents/Projects/ontario-job-portal/routes/employer.js:68:68)
at <anonymous>

Because for some reason, req.params.emp_id is [object Object]. If I try to JSON.stringify I get "[object Object]", so that's extremely unhelpful.

Here's the _id field from mongoDB

"_id": {
    "$oid": "5bc9d28270e5375dfbfe17fd"
},

So how can I pass the _id of a user as a string?

1 Answer 1

1

To get hexadecimal representation of id you can use .valueOf(). More methods and examples here: https://docs.mongodb.com/manual/reference/method/ObjectId.valueOf/#ObjectId.valueOf

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.