【问题标题】:Can't populate using mongoosejs无法使用 mongoosejs 填充
【发布时间】:2017-02-13 08:12:37
【问题描述】:

为什么我在尝试填充我的方案时无法获得结果(我使用 mongoosejs)。在我的情况下,我的类别、子类别、子类别方案不使用 _id。我使用自定义 ID。

这是我的产品方案:

.....
categoryId: {
  type: String,
  ref: 'Catgory',
  required: true
},
subcategoryId: {
  type: String,
  ref: 'Subcategory',
  required: true
},
subsubcategoryId: {
  type: String,
  ref: 'Subsubcategory',
  required: true
});

const Product = mongoose.model('Product', product);
module.exports = Product;

这是我的产品控制器:

'getOne': function(req, res, next) {
    if (typeof req.params.id !== 'string') return res.send({
        'error-code': 3
    });

    Product.findOne({
            _id: req.params.id
        })
        .populate({
            path: 'category',
            select: 'name'
        })
        .populate({
            path: 'subcategory',
            select: 'name'
        })
        .populate({
            path: 'subsubcategory',
            select: 'name'
        })
        .exec(function(error, response) {
            if (error) return handleError(error);
            return res.send(response);
        })
}

非常感谢您的帮助。

【问题讨论】:

    标签: node.js mongoose restify mongoose-schema mongoose-populate


    【解决方案1】:

    不幸的是,截至目前,您只能在 populate 字段中使用外国 _ids。很长一段时间以来,按不同字段填充heavily requested feature on the mongoose GitHub

    【讨论】:

    • 哦,非常感谢。我会用其他方法再试一次。
    猜你喜欢
    • 2015-11-09
    • 2017-10-08
    • 1970-01-01
    • 2015-01-20
    • 2015-10-23
    • 2018-03-05
    • 2014-09-20
    • 2018-06-04
    • 1970-01-01
    相关资源
    最近更新 更多