2015-07-21 190 views
0

全部。猫鼬不填充先前保存的文档参照新保存的文档

我正在编写一个MEAN栈应用程序,使用Mongoose(4.0.6)和Node/Express来与MongoDB接口,并且当我保存新文档时,现有文档应该有一个保存文档参考。具体来说,在应用程序中,我有一个用户在为该公司创建其管理员帐户之前创建了一个公司实例,因此当用户注册为管理员时,我希望公司文档使用新用户。

这里是我的公司和用户模式:

user.js的...

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

var userSchema = new Schema({ 
    first_name:  { type: String, required: '{PATH} is required!' }, 
    last_name:  { type: String, required: '{PATH} is required!' }, 
    username:  { type: String, required: '{PATH} is required!', lowercase: true, unique: true }, 
    password:  { type: String, required: '{PATH} is required!' }, 
    roles:   { type: [String] }, 
    company:   { type: ObjectId, ref: 'Company', required: true }, 
    db_permissions: [{ type: ObjectId, ref: 'DataConnection' }], 
    created_by:  { type: ObjectId, ref: 'User' }, 
    created_at:  { type: Date, default: Date.now }, 
    updated_at:  [{ type: Date, default: Date.now }] 
}); 

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

module.exports = { 
    User: User 
}; 

Company.js ...

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

var companySchema = new Schema({ 
    name:    { type: String, uppercase: true, required: '{PATH} is required!', unique: true }, 
    industry:   { type: String, required: '{PATH} is required!' }, 
    phone:    { type: String, required: '{PATH} is required!' }, 
    address_line_1: { type: String, uppercase: true, required: '{PATH} is required!' }, 
    address_line_2: { type: String, uppercase: true }, 
    city:    { type: String, uppercase: true, required: '{PATH} is required!' }, 
    state_prov:  { type: String, uppercase: true, required: '{PATH} is required!' }, 
    postal_code:  { type: String, required: '{PATH} is required!' }, 
    country:   { type: String, required: '{PATH} is required!' }, 
    logo_url:   String, 
    users:   [{ type: ObjectId, ref: 'User' }], 
    data_connections: [{ type: ObjectId, ref: 'DataConnection' }], 
    created_at:  { type: Date, default: Date.now }, 
    updated_at:  [{ type: Date, default: Date.now }] 
}); 

var Company = mongoose.model('Company', companySchema); 

module.exports = { 
    Company: Company 
}; 

这里是我的控制器代码:

User.create(userData, function(err, user) { 
    if(err) { 
    if(err.toString().indexOf('E11000') > -1) { 
     err = new Error('Duplicate email'); 
    } 
    res.status(400); 
    return res.send({ reason:err.toString() }); 
    } 
    console.log('company id: ' + user.company); 
    Company.findById(user.company) 
    .populate({path: 'users'}) 
    .exec(function (err, company) { 
     if (err) return handleError(err); 
     console.log(company.name + '\'s users now includes ' + company.users); 
    }); 
    res.send(user); 

该公司y(例如TEST53)保存到数据库中正确使用空用户阵列:

{ 
    "_id": "55ae421bf469f1b97bb52d5a", 
    "name": "TEST53", 
    "industry": "Construction", 
    "phone": "2352626254", 
    "city": "GDFGD", 
    "country": "United States", 
    "address_line_1": "DSGDFGH", 
    "state_prov": "GF", 
    "postal_code": "45645", 
    "logo_url": "", 
    "__v": 0, 
    "updated_at": [ 
     "2015-07-21T12:59:07.609Z" 
    ], 
    "created_at": "2015-07-21T12:59:07.597Z", 
    "data_connections": [], 
    "users": [] 
} 

然后,当我创建的用户,它可以正确保存:

{ 
    "_id": "55ae4238f469f1b97bb52d5b", 
    "username": "[email protected]", 
    "password": "$2a$12$ZB6L1NCZEhLfjs99yUUNNOQEknyQmX6nP2BxBvo1uZGlvk9LlKGFu", 
    "company": "55ae421bf469f1b97bb52d5a", 
    "first_name": "Test53", 
    "last_name": "Admin", 
    "__v": 0, 
    "updated_at": [ 
     "2015-07-21T12:59:36.925Z" 
    ], 
    "created_at": "2015-07-21T12:59:36.550Z", 
    "db_permissions": [], 
    "roles": [ 
     "admin" 
    ] 
} 

而且我可以看到正确的ObjectId打印到控制台对于user.company:

company id: 55ae421bf469f1b97bb52d5a 

但该公司的用户数组不与用户的ID进行填充,而.exec函数打印内的console.log“TEST53的用户现在包括”。

我已经尝试了几种方式将它连接起来,只用'users'而不是{path:'users'},编写一个将用户推入数组的函数,但使用.run而不是.exec远未成功。

有什么明显的我失踪了吗?在此先感谢您的任何建议!

回答

1

你实际上并没有将用户添加到公司。

试试这个:

Company.findById(user.company, function (err, company) { 
    if (err) return handleError(err); 

    // Add the user to the company's list of users. 
    company.users.push(user); 

    // Need to save again. 
    company.save(function(err) { 
    if (err) return handleError(err); 
    console.log(company.name + '\'s users now includes ' + company.users); 
    }); 
}); 
res.send(user); 

在我看来,所有你想要做的是更新Company模型添加的用户,而不是实际使用(居住)Company文件作为响应,所以我省了一个额外的Company.findById(...).populate(...)电话。

+0

是的,这样做!非常感谢。这是我第一次感受通过堆栈中所有不同技术的方式,所以我非常欣赏这个指导。 –