【问题标题】:how to get arrays from refs with mongoose如何使用 mongoose 从 refs 获取数组
【发布时间】: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


    【解决方案1】:

    //category.js

    var mongoose = require('mongoose');
    
    var CategorySchema = new mongoose.Schema({
        name: { type: String, default: '' },
        products: [{productID: {type : mongoose.Schema.Types.ObjectId, ref :      'Product'}}]
    });
    
    module.Category = mongoose.model("Category", CategorySchema);
    

    //product.js

    var mongoose = require('mongoose');
    
    var ProductSchema = new mongoose.Schema({
        name: { type: String, default: '' },
        price: { type: Number, default: 0 }
    });
    
    module.Product = mongoose.model("Product", ProductSchema);
    

    //一些文件

    var Product = require('./Product').Product,
        Category = require('./Category').Category;
    
    var product = new Product({
        name: "Some name",
        price: 100
    });
    
    product.save(function(err){
        if(err) throw err;
    });
    
    
    *//creating*
    var category = new Category({
        name: "Some name",
        products: [{
             productID: product._id
        },{
    .....///and so on.....
        }]
    });
    
    *//saving*
    category.save(function(error) {
        if (!error) {
            Category.find({})
                .populate('products.productID')
                .exec(function(error, cats) {
                    console.log(JSON.stringify(cats, null, "\t"))
                })
        }
    });
    

    希望对你有帮助。

    【讨论】:

    • Category.find.populate 显示与 Category.find() 相同的数据
    猜你喜欢
    • 2013-11-06
    • 2011-11-20
    • 2019-07-17
    • 2023-03-11
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 2021-02-11
    相关资源
    最近更新 更多