【发布时间】:2016-10-17 21:40:53
【问题描述】:
我的问题是当我find() and print 我的条目时,数组不会炫耀。这是一个问题,因为我需要发送我的 API。
这是我得到的结果:
{
"_id": "576287e5a3536e670a39f3f2",
"__v": 0,
"products": [],
"name": "SISI CEST ICI"
},
{
"_id": "5762897e3032fe7c0a1d2408",
"__v": 0,
"products": [],
"name": "hElo la mif"
}
这是我得到这个结果的方法:
var response = {};
mongoose.cat.find({},function(err,data){
console.log(data);
// Mongo command to fetch all data from collection.
if(err) {
response = {"error" : true,"message" : "Error fetching data"};
} else {
response = {"error" : false,"message" : data};
}
res.json(response);
});
模型如下所示:
var category = new mongoose.Schema({
id: mongoose.Schema.ObjectId,
name: { type: String, default: '' },
products: [ {type : mongoose.Schema.ObjectId, ref : 'Product'} ]
});
var product = new mongoose.Schema({
id: mongoose.Schema.ObjectId,
name: { type: String, default: '' },
price: { type: Number, default: '' }
});
var restoModel = mongoose.model('Resto', resto);
var MenuModel = mongoose.model('Menu', menu);
var CategoryModel = mongoose.model('Category', category);
var ProductModel = mongoose.model('Product', product);
module.exports = {
resto: restoModel,
menu: MenuModel,
cat: CategoryModel,
product: ProductModel
}
而在我的数据库中,字段 products 不是空的,屏幕可以显示:
所以我的问题是:如何访问我的产品数组中的数据?因为我需要他们寄回去。
我尝试了"Populate query",但它没有提供任何信息。
我的问题是,我的主要类别中的所有数据如何以 Json 格式保存?
【问题讨论】:
标签: arrays node.js mongodb express mongoose