Mongodb 无法使用mongoose populate()填充文档

Mongodb 无法使用mongoose populate()填充文档,mongodb,mongoose-populate,Mongodb,Mongoose Populate,我正在用猫鼬在express中申请。我有一个名为users的集合,其中有一个名为\u subscriptions的字段,它是一个对象数组,每个对象都包含一个名为field的字段,它是字段的文档的ObjectId(这是我数据库中的另一个集合) 我想制作这样一个API,它在获取id参数后返回一个user表单users集合,并填充field属性,而不是字段field值中的id。为此,我使用了填充方法,但它不起作用 这是显示用户集合的屏幕截图: 这是显示字段的屏幕截图集合: 这是字段的模式(文件名f

我正在用猫鼬在express中申请。我有一个名为
users
的集合,其中有一个名为
\u subscriptions
的字段,它是一个对象数组,每个对象都包含一个名为
field
的字段,它是
字段的文档的ObjectId(这是我数据库中的另一个集合)

我想制作这样一个API,它在获取id参数后返回一个
user
表单
users
集合,并填充
field
属性,而不是字段
field
值中的id。为此,我使用了填充方法,但它不起作用

这是显示
用户
集合的屏幕截图:

这是显示
字段的屏幕截图
集合:

这是字段的模式(文件名field.js):

这是用户的模式和模型

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
    _id: Schema.Types.ObjectId,
    salt: String,
    provider: String,
    name: String,
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    _subscriptions: [{
        field: {
            type: mongoose.Schema.ObjectId,
            ref: 'Field',
        },
        status: String,
        dateSubscribed: Date,
        payments: [{}]
    }],
    role: String,
});

module.exports = mongoose.model('User', UserSchema);
这是用户路由器的代码

var Field = require('../model/Field');
var express = require('express');
var router = express.Router();
var User = require('../model/User');

router.get('/',function(req, res, next) {
    User.find({}, function(err, result) {
        if (err) {
            console.log(err);
            res.send('something wrong');
        }
        res.status(200).send(result);
    }).populate( '_subscriptions.field').exec(function (err, story) {
        if (err) return handleError(err);
        console.log('Here!!!!!');
      });
});

router.get('/findById/:id',function(req, res, next) {
    var id = req.params.id;
    User.findById(id, function(err, doc) {
      if (err) {
        console.error('error, no entry found');
      }
      res.status(200).send(doc);
    }).populate('field').exec(function (err, story) {
        if (err) return handleError(err);
        console.log('Here!!!!!');
    });
});

router.get('/getSubscriptions/:id',function(req, res, next) {
    var id = req.params.id;
    User.findById(id, function(err, doc) {
      if (err) {
        console.error('error, no entry found');
      }
      var type = typeof(doc);
      res.status(200).send(doc);
    })
});
 module.exports = router;
这就是我调用app.use方法的地方:

这是我用邮递员得到的回应

我期待着有人帮助解决这个问题 因为我无法识别我的错误。我们将非常感谢你在这方面的帮助


提前感谢

据我所知,在用户集合中有
\u订阅
,在
\u订阅
中有
字段
。如果这是您的架构,则应将“
\u subscriptions.field
”作为参数传递给填充函数,而不是当前传递的“
field

因此,用户子路由的代码,
/findById/:id
,必须如下所示:

router.get('/findById/:id',function(req, res, next) {
    var id = req.params.id;
    User.findById(id, function(err, doc) {
      if (err) {
        console.error('error, no entry found');
      }
      res.status(200).send(doc);
    }).populate('_subscriptions.field').exec(function (err, story) {
        if (err) return handleError(err);
        console.log('Here!!!!!');
    });
});

简化你的询问你是对的@Muhammad Danish,它现在可以工作了。谢谢
router.get('/findById/:id',function(req, res, next) {
    var id = req.params.id;
    User.findById(id, function(err, doc) {
      if (err) {
        console.error('error, no entry found');
      }
      res.status(200).send(doc);
    }).populate('_subscriptions.field').exec(function (err, story) {
        if (err) return handleError(err);
        console.log('Here!!!!!');
    });
});