I'm working on a online shop project. I'm using Node.js, express.js and MongoDB with mongoose. I got the product information from the MongoDB database and sending them to the client side. In my case, I can get all these data in my client side but before sending, when I print them to the console in server side, it says undefined.
This is the products schema:
var schema = new Schema({
imagePath: {
type: String,
required: true
},
productName: {
type: String,
required: true
},
productPrice: {
type: Number,
required: true
},
productCategory: {
type: String,
required: true
},
productShortInformation: {
type: String,
required: true
},
productFullInformation: {
type: String,
required: true
},
productViews: {
type: Number,
required: false
},
productStock: {
type: Number,
required: true
}
});
and here is my Node.js code
router.get('/category/summary', function(req, res, next) {
//getting my all products information
var products = Product.find(function (err, docs) {
if(err) {
console.log('Error Happened' + err);
return res.redirect('/');
} else {
//HERE IS THE PROBLEM
//ALL PRODUCT NAME IS SHOWN UNDEFINED
//BUT WHEN I SEND THEM TO THE CLIENT, I GET PRODUCT NAME
for(var product in docs) {
console.log('Name: ' + product.productName);
}
res.render('shop/categorySummary', {
products: docs //sending these information to the client side
});
}
});
});
When I try to print these product name, I get undefined. But in the client side I can print the product information.
I need to manipulate these data before sending them to the client side. So how can I print these product information to the server side(in console) before sending?