0

I only want to pull data unique to a user where my req.user === foundListing.uid. How can I adjust this function to only show those objects?

function show(req, res) {
  db.User.findById(req.user, function (err, user) {
    console.log(req.user)
    if (err) {console.log(err);}
    db.Listing.findById(req.params.listingId, function(err, foundListing) {
      console.log(foundListing.uid );
      if(err) { console.log('listingsController.show error', err); }
      res.json(foundListing);
    });
  })
}
1
  • 1
    Why do you select user first, and after that listings? If listings is related to user you can create only one request, otherwise this select is unneeded. Commented Sep 6, 2017 at 16:09

1 Answer 1

1

Use find().

function show(req, res) {
  db.User.findById(req.user, function (err, user) {
    console.log(req.user)
    if (err) {console.log(err);}
    db.Listing.find({ uid: req.user }, function(err, listings) {
      console.log(listings);
      if(err) { console.log('listingsController.show error', err); }
      res.json(listings);
    });
  })
}

I don't know if you can directly use req.user. Normally, I use req.user._id.

Sign up to request clarification or add additional context in comments.

3 Comments

@KennethVo It worked with just req.user too? (I'm assuming req.user is an object and not an ID.)
Yes it worked with req.user. Was following a Satellizer tutorial and that's how it was set up so I guess it was actually an object.
@KennethVo Ah interesting. We both learned something new today. Kindly mark your question as answered.

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.