My code was working fine but after taking fresh code I am getting this error but as I can see my id is valid Object Id
Cast to ObjectId failed for value "5848e9ecaec0f31372816a26" at path "_id" for model "user"
how to fix this?
You have to cast the data to mongodb object id. If you are using mongoose, you can actually do these below.
const mongoose = require('mongoose');
const ObjectId = mongoose.Types.ObjectId;
[your_mongodb_model/collection].query({ _id: ObjectId(id) });
or simply, you can do these,
[your_mongodb_model].query({ _id: mongoose.Types.ObjectId(id) });
The reason for this error is documented in this mongoose issue. A workaround is to downgrade to mongoose version <= 4.7.2 or use Node version >=6.