W
W
Wasya UK2017-04-28 12:23:23
MongoDB
Wasya UK, 2017-04-28 12:23:23

How to compare variable with ObjectId (mongoose)?

I get an ID, but I don't know how to compare if it's correct.

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;

var id = req.params.id;
    
if (id instanceof ObjectId) {
    console.log('yes');
} else {
    console.log('no');
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wasya UK, 2017-04-28
@dmc1989

Problem solved!

var id = req.params.id;
    
    if (mongoose.Types.ObjectId.isValid(id)) {
        console.log('yes');
    } else {
        console.log('no');
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question