0

I want to query my MongoDB database to only fetch all the values in my array using my user_id

I have tried using the (schema.name), it not working out

// @route   GET api/user/package
// @desc    Get all package
// @access  Private
router.get('/package', 
  passport.authenticate('jwt', { session: false }),

 (req, res) => {
  Shipments.find({_id : req.user._id},{paymentStatus: "incomplete"})
    .then(shipments => res.status(200).json(shipments.packages))
    .catch(err =>res.status(404).json({ nopackages: 'No package found for you now' }));
});

Here is my expected result

"packages": [
    {
      "date": "2019-09-23T12:52:14.226Z",
      "_id": "5d88bffe2a6ed7b8d9873548",
      "category": "hazard",
      "quantity": "10",
      "description": "a valueablegoods",
      "length": 10,
      "height": 20,
      "width": 12,
      "weight": 12
    },
    {
      "date": "2019-09-23T12:52:58.129Z",
      "_id": "5d88c02a2a6ed7b8d9873549",
      "category": "hazard",
      "quantity": "10",
      "description": "a valueablegoods",
      "length": 10,
      "height": 20,
      "width": 12,
      "weight": 12
    }
  ],

but postman didn't return any value but brings a status 200, but returns nobody

2 Answers 2

2

Try Shipments.findOne()

This happens because you are getting a single user detail.

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

2 Comments

i already tried it joy but it is not working, so glad you are Nigerian lady.
Not that it is not working, it is not just returning the res.json of the package. but if i request for the shipment, it returns only the _id and the payment status
1

All i did was change find to findOne. This solve the problem

router.get('/package', 
  passport.authenticate('jwt', { session: false }),

 (req, res) => {
  Shipments.findOne({ $and: [{ _id : req.user._id }, { paymentStatus: "incomplete" }] })
    .then(shipments => res.status(200).json(shipments.packages))
    .catch(err =>res.status(404).json({ nopackages: 'No package found for you now' }));
});

it may help u

1 Comment

it only bring the whole info when i do .then(shipments => res.status(200).json(shipments)) but if i do .then(shipments => res.status(200).json(shipments.packages)) it return an empty body.

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.