node-restful-api-tutorial icon indicating copy to clipboard operation
node-restful-api-tutorial copied to clipboard

Empty JSON Response on populating orders by Product Model

Open consciousnessdev opened this issue 5 years ago • 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
  });
 });
});

consciousnessdev avatar Apr 23 '19 09:04 consciousnessdev

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

ma-9 avatar Nov 04 '19 08:11 ma-9

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 : https://github.com/academind/node-restful-api-tutorial/issues/30

Ram-143-sai avatar Apr 09 '23 07:04 Ram-143-sai