猫鼬“填充"不填充 [英] Mongoose 'populate' not populating

查看:92
本文介绍了猫鼬“填充"不填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在MEAN堆栈上构建一个相当简单的应用程序,我的确超出了我的深度,尤其是当涉及到猫鼬时.我发现猫鼬的文档很难缠住我的头,在其他任何地方都找不到答案.

I am building a fairly simple application on the MEAN stack and I am really out of my depth, especially when it comes to mongoose. I have found the mongoose documentation very difficult to wrap my head around and cannot find answers anywhere else.

我的问题是:我有一堆用户,这些用户有存储库,而存储库有存储库提供程序(GitHub,BitBucket等).

My issue is this: I have a bunch of users, these users have repositories and the repositories have repository providers (GitHub, BitBucket etc).

用户有很多存储库,而一个存储库只有一种存储库类型.

A user has many repositories and a repository has one repository type.

我的用户文件包含以下内容:

My user file contains the following:

var mongoose = require('mongoose'),
Schema   = mongoose.Schema; 

var UserSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true
    },
    repositories: [{
        name: String,
        branches: [{
            name: String,
            head: Boolean,
            commits: [{
                hash: String,
                date: Date,
                message: String,
                contributer: String,
                avatar: String
            }]
        }],
        repoType: { 
            type: Schema.Types.ObjectId, 
            ref: 'RepoProviders'
        }
    }]
});

var User = mongoose.model('User', UserSchema);

module.exports = User;

// This is where the magic doesn't happen :(

User.find({ name: "John Smith"}).populate({path: 'repoType'}).exec(function (err, user) {
  if (err) return handleError(err);
  console.log(user);
});

RepoProvider.js包含:

RepoProvider.js contains:

var mongoose = require('mongoose');
Schema = mongoose.Schema;

var RepoProviderSchema = new Schema({
    name: {
        type: String,
        required: true
    }
});

var RepoProvider = mongoose.model('RepoProviders', RepoProviderSchema);
module.exports = RepoProvider;

我正在mongo中创建用户文档,并手动分配一个repoType id哈希(取自现有的repoType文档).

I am creating user documents in mongo and manually assigning a repoType id hash (taken from an existing repoType document).

当我console.log用户时,回购类型设置为id,但是没有返回任何关系:

When I console.log the User, the repo type is set to the id but there is no relationship returned:

[ { _id: 5547433d322e0296a3c53a16,
    email: 'john@smith.com',
    name: 'John Smith',
    __v: 0,
    repositories: 
     [ { name: 'RepoOne',
         repoType: 5547220cdd7eeb928659f3b8,
         _id: 5547433d322e0296a3c53a17,
         branches: [Object] } ] } ]

如何正确设置和查询此关系?

How do I properly set and query this relationship?

推荐答案

您需要在

这篇关于猫鼬“填充"不填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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