[英]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.