[英]How to print data, retrieved from mongoDB database in Node.js?
我正在从事在线商店项目。 我正在将Node.js,express.js和MongoDB与猫鼬一起使用。 我从MongoDB数据库中获得了产品信息,并将其发送到客户端。 就我而言,我可以在客户端获取所有这些数据,但是在发送之前,当我将它们打印到服务器端的控制台时,它说未定义。
这是产品架构:
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
}
});
这是我的Node.js代码
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
});
}
});
});
当我尝试打印这些产品name
,我不确定。 但是在客户端,我可以打印产品信息。
我需要先处理这些数据,然后再将它们发送到客户端。 那么如何在发送之前将这些产品信息打印到服务器端(在控制台中)?
for(var product in docs) {
console.log('Name: ' + docs[product].productName);
}
那应该工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.