node-restful-api-tutorial
node-restful-api-tutorial copied to clipboard
Empty JSON Response on populating orders by Product Model
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
});
});
});
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 : https://github.com/academind/node-restful-api-tutorial/issues/30