0
我正在開展在線商店項目。我使用了mongoose來使用Node.js,express.js和MongoDB。我從MongoDB數據庫獲取產品信息並將它們發送到客戶端。就我而言,我可以在客戶端獲取所有這些數據,但在發送之前,當我將它們打印到服務器端的控制檯時,它說未定義。如何打印數據,從Node.js中的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
,我得到了一個未定義。但在客戶端,我可以打印產品信息。
我需要在將這些數據發送到客戶端之前處理這些數據。那麼如何在發送之前將這些產品信息打印到服務器端(在控制檯中)?
媽呀,非常感謝你。它正在工作。非常感謝。 –
@BShammNahid在Stack Overflow上,我們表示感謝你[**接受幫助我們的答案](http://stackoverflow.com/help/accepted-answer)。在左邊的答案旁邊有一個綠色的選中標記,您勾選:) – chridam