我正在尝试从用户模型获取填充数据。但是,人口没有工作。 我期待用户模型提供Cat数据。用户中没有猫数据。
这是我的猫模式。
const mongoose = require('mongoose');
const catSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
accessibility: {
type: String,
required: true,
},
friendliness: {
type: String,
required: true,
},
likes: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
}],
founder: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
comments:[{
type: mongoose.Schema.Types.ObjectId,
ref: 'Comment',
}],
location: [],
image: {
type: String,
required: true,
},
time: {
type: String,
},
description: {
type: String,
default: '',
}
});
module.exports = mongoose.model('Cat', catSchema);
这是我的用户架构。
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
facebookId: {
type: String,
required: true,
},
name: {
type: String,
required: true,
},
cats: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Cat',
}],
});
module.exports = mongoose.model('User', userSchema);
并且我使用这段代码来获取填充数据,但是它不起作用
const user = await User.findById(id).populate('Cat');