Reputation: 486
Let's say I have these two models below and i want to get or fetch the location of a specific user using mongoose in node:
User Model
const userSchema = new mongoose.Schema({
name: {
type: String,
minlength: 2,
maxlength: 50,
},
email: {
type: String,
minlength: 5,
maxlength: 255,
unique: true,
},
password: {
type: String,
minlength: 5,
maxlength: 1024,
},
});
And Location Model
const locationtSchema = new mongoose.Schema({
name: {
type: String,
minlength: 5,
maxlength: 50,
},
lat: {
type: String,
required: true,
},
lng: {
type: String,
required: true,
},
user: {
type: userSchema,
required: true,
},
});
So, I tried to resolve it like this... but it didn't work.
const location = Location.find({
'user._id': req.params._id
})
How can i return locaitons of a specific user?
Upvotes: 0
Views: 37
Reputation: 140
The id you are trying to pass is a string, instead, it should be an object id
const location = Location.find({
'user._id': mongoose.Types.ObjectId(req.params._id)
})
It would work perfectly now.
Upvotes: 1