1

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

3
  • 1
    .get will not work, you need to remove that console statement from there, you can place it inside the then() block Commented Feb 1, 2018 at 9:45
  • @KiranLM, I can't understand what you are asking in your comment Commented Feb 1, 2018 at 9:48
  • 1
    I've edited the comment, you need to place that console.log inside then block Commented Feb 1, 2018 at 9:50

1 Answer 1

1

This code is wrong

Favorites.find({user: req.user.id})
console.log(req.user.id);
//.populate('user')
//.populate('cars')
.then((favorites) => {

You have to apply .then to the promise, and now you are applying it to nothing. Try this:

console.log(req.user.id);
Favorites.find({user: mongoose.Schema.Types.ObjectId(req.user.id)})
.then((favorites) => {
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer. You are right, but it was my fault when posting the question. The console.log() statement had to be placed before Favorites.find(......). It is correctly placed in the code that I am executing
It could be because you aren't finding with ObjectId, but with string. I updated the code
Ohh... I finally found where the problem was. It has not to do with the code. I did the post operation with one user, and then did the get operation with another user, which really didn't have any 'favourite' stored. Thank you all

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.