What I have
I have defined a mongoose schema as follows
var favoriteSchema = new Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
cars: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Car'
}]
}, {
timestamps: true
});
I have also defined get/post routes for this model/schema:
favoriteRouter.route('/')
.get((req,res,next) => {
Favorites.find({user: req.user.id})
console.log(req.user.id);
//.populate('user')
//.populate('cars')
.then((favorites) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(favorites);
}, (err) => next(err))
.catch((err) => next(err));
})
.post((req, res, next) => {
Favorites.create(req.body)
.then((favorite) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(favorite);
}, (err) => next(err))
.catch((err) => next(err));
})
What I need
The 'get' operation is not working for me. I would like to retrieve all the favourites of a user. The post request is working fine, so I can see the documents stored correctly with the mongo console, but when I perform the get request, I get an empty array.
What I tried
· I tried to console.log(req.user.id) to confirm that was correct.
· I also tried not to filter by user -> Favorites.find({}) and this works fine.
· I tried Favorites.find({'name': ...}) and Favorites.find({name: ...}) but none of them work for me either.
Any help would be appreciated. Thanks