[英]mongoose populate reference returns undefined
I am fairly new to mongoose so I have probably missed something here. 我是猫鼬的新手,所以我可能在这里错过了一些东西。
I have two Collections "Company" & "User" I am trying to get all Users who belong to a company but The users array on the company is returning undefined instead of what I would expect to be the user objects. 我有两个集合“公司”和“用户”,我试图获取属于公司的所有用户,但是公司上的用户数组返回的是undefined而不是我希望成为的用户对象。
I have read through the documentation and populate seemed to be a step in the right direction however, it doesn't mention at any stage (that I could see) how to save into an array I assume I need to push the objects to the emails property on the user object? 我已经阅读了文档,并且似乎是朝着正确方向迈出的一步,但是,它在任何阶段都没有提到(我可以看到)如何保存到数组中,我认为我需要将对象推送到电子邮件中用户对象上的属性?
I come from a very mysql heavy background and I maybe doing somethings incorrectly would appreciate if someone could explain how MongoDB handles relationships? 我来自非常繁杂的mysql背景,如果有人可以解释MongoDB如何处理关系,也许我做错了一些事情,将不胜感激。
Company Schema 公司架构
const companySchema = new Schema({
name: String,
slug: String,
_creator: { type: Schema.Types.ObjectId, ref: 'User' },
users: [{ type: Schema.Types.ObjectId, ref: 'User' }],
created_at: Date,
updated_at: Date
});
module.exports = mongoose.model('Company', companySchema);
User Schema 用户架构
const userSchema = new Schema({
first_name: String,
last_name: String,
username: String,
password: String,
companies: [{ type: Schema.Types.ObjectId, ref: 'Company' }],
created_at: Date,
updated_at: Date
});
module.exports = mongoose.model('User', userSchema);
Save the user 保存用户
const dave = new User({
first_name: 'Dave',
last_name: 'Hewitt',
username: 'moshie',
password: '123456789',
updated_at: new Date()
});
dave.save()
.then(function (user) {
const indigoTree = new Company({
name: 'IndigoTree',
slug: 'indigotree',
_creator: dave._id,
updated_at: new Date()
});
indigoTree.users.push(user);
return indigoTree.save();
})
.then(function (company) {
console.log(company);
})
.catch(function (error) {
console.log(error);
});
Check the user 检查用户
Company.find({}).populate('users').exec()
.then(function (doc) {
doc.users // undefined?
});
Any ideas? 有任何想法吗?
As per API Docs , Mongoose's find()
returns an array collections instead of single item. 根据API Docs ,Mongoose的find()
返回数组集合而不是单个项目。
For findOne() it is a potentially-null single document, find() a list of documents, count() the number of documents, update() the number of documents affected, etc 对于findOne(),它是一个可能为空的单个文档,find()文档列表,count()文档数量,update()受影响的文档数量等
Company.find({}).populate('users').exec().then((doc) => {
console.log(doc[0].users); // prints users array
});
You are pushing user
into the users
array. 您正在将user
推送到users
数组。 Instead of that you need to push
user's Id
into the array ie user._id
. 取而代之的是,您需要push
user's Id
push
入数组即user._id
。
Replace: 更换:
indigoTree.users.push(user);
With: 附:
indigoTree.users.push(user._id);
Also, find()
query returns array of documents
, so you need to use doc[0].users
, not doc.users
. 另外, find()
查询返回array of documents
,因此您需要使用doc[0].users
,而不是doc.users
。
Company.find({}).populate('users').exec()
.then(function (doc) {
doc[0].users // undefined? -> change here, it wont come undefined
});
Alternatively , you can use findOne()
instead of find()
, which returns an object
. 或者 ,可以使用findOne()
代替find()
来返回一个object
。 In that case you can use doc.users
在这种情况下,您可以使用doc.users
Company.findOne({_id: someCompanyId}).populate('users').exec()
.then(function (doc) {
doc.users // it wont come undefined
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.