Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty JSON Response on populating orders by Product Model #17

Open
consciousnessdev opened this issue Apr 23, 2019 · 2 comments
Open

Empty JSON Response on populating orders by Product Model #17

consciousnessdev opened this issue Apr 23, 2019 · 2 comments

Comments

@consciousnessdev
Copy link

i got empty json when access get to :http://localhost:3000/orders
{
"count": 0,
"orders": []
}

order.js :

const mongoose = require('mongoose');
const orderSchema = mongoose.Schema({
 _id: mongoose.Schema.Types.ObjectId,
 product: {type:mongoose.Schema.Types.ObjectId, ref:'Product', required: true},
 quantity: {type:Number, default:1}
});

module.exports = mongoose.model('Order',orderSchema)

getroutes :

router.get('/', (req,res,next)=>{
 Order.find()
  .select("product quantity _id")
  .populate('product')
  .exec()
  .then(docs => {
   res.status(200).json({
    count: docs.length,
    orders: docs.map(doc => {
     return {
      _id :doc._id,
      product: doc.product,
      quantity: doc.quantity,
      request: {
       type: 'GET',
       url: `http://localhost:3000/orders/${doc._id}`
      }
     };
    })
   });
  })
 .catch(err => {
  res.status(500).json({
   error: err
  });
 });
});
@ma-9
Copy link

ma-9 commented Nov 4, 2019

push your project on Github and leave link here
I'll check it out bro

@Ram-143-sai
Copy link

your get request code is working fine I had gone through the code you posted. There might be a chance of having an issue with your post request as you haven't created any order or there might be an error with your post code.
check this issue for reference : #30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants