academind/node-restful-api-tutorial

Empty JSON Response on populating orders by Product Model

consciousnessdev opened this issue · 2 comments

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 commented

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

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