为什么我的裁判不填充文档? [英] Why won't my refs populate documents?

查看:47
本文介绍了为什么我的裁判不填充文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Node.js应用程序中使用Mongoose 4.3.6.

I am using Mongoose 4.3.6 in a Node.js app.

例如,我有两个模型:

var User = mongoose.model('User', SomeParentSchema.extend({
  _id: String,
  name: String
}));

var Like = mongoose.model('Like', AnotherParentSchema.extend({
  _id: String,
  user_id: {
    type: String,
    ref: 'User'
  }
}));

我也在使用 mongoose-schema-extend ,我们可以说出于这个问题,两个实体都从某个父架构扩展而来.

I am also using mongoose-schema-extend, and we can say for the sake of this question that both entities extend from some parent schema.

由于我使用的是ref,因此理论上 Model.populate 应该自动填充User模型的字段.

Since I am using a ref, in theory Model.populate should auto-populate the field with a User model.

ref选项可以告诉Mongoose在填充过程中使用哪种模型

The ref option is what tells Mongoose which model to use during population

Like.findOne({}).populate('user_id').exec(callback);

以上内容永远无法使用.如此简单(该字段以null结尾).我必须这样做才能使其正常工作:

The above just never works. Simple as that (the field ends up as null). I have to do this to make it work:

Like.findOne({}).populate({ path: 'user_id', model: 'User' }).exec(callback);

执行此操作时,该字段是预期的User对象(但是由于某些原因,我不得不明确地告诉populate将其作为User模型查找该路径;即使应该使用ref根据我上面链接的Mongoose文档来解决这个问题.

When I do that, the field is the expected User object (but for some reason I had to explicitly tell populate to look for that path as a User model; even though the ref is supposed to handle that, according to the Mongoose docs I linked to above).

我想念什么吗?

推荐答案

我仅使用以下代码测试您的示例代码,该查询Like.findOne({}).populate('user_id').exec(callback);似乎运行良好.

I just test your sample codes with following, this query Like.findOne({}).populate('user_id').exec(callback); seems work well.

模式:

var PersonSchema = new mongoose.Schema({
    t: String
}, {collection: 'persons'});

var User = mongoose.model('User', PersonSchema.extend({
  _id: String,
  name: String
}));

var ParentSchema = new mongoose.Schema({
    s: String
}, {collection: 'parent'});

var Like = mongoose.model('Like', ParentSchema.extend({
  _id: String,
  user_id: {
    type: String,
    ref: 'User'
  }
}));

将数据插入数据库,

var user = new User({
    t: 't1',
    _id: '1234567',
    name: 'test'
});

var like = new Like({
    s: 's1',
    _id: '23456789',
});

user.save(function(err, u){
    if(err)
        console.log(err);
    else {
        like.user_id = u._id;
        console.log(like);
        like.save(function(err) {
            if (err)
                console.log(err);
            else
                console.log('save like and user....');
        });
    }
});

查询

Like.findOne({}).populate('user_id').exec(function(err, doc) {
    if (err)
        console.log(err);
    else
        console.log(doc);
});

结果是

{ _id: '23456789',
  __t: 'Like',
  user_id: { _id: '1234567', __t: 'User', t: 't1', name: 'test', __v: 0 },
  s: 's1',
  __v: 0 }

这篇关于为什么我的裁判不填充文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆