Javascript 猫鼬不繁殖
我试图填充我的一个模型,但它不起作用。 这是我的卡片模式:Javascript 猫鼬不繁殖,javascript,mongodb,express,mongoose,Javascript,Mongodb,Express,Mongoose,我试图填充我的一个模型,但它不起作用。 这是我的卡片模式: const CardSchema = new mongoose.Schema({ text: { type: String, }, wrap: { type: String, }, user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', } }); module.exports = mongoose.model('Ca
const CardSchema = new mongoose.Schema({
text: {
type: String,
},
wrap: {
type: String,
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
}
});
module.exports = mongoose.model('Card', CardSchema);
这是我的控制器:
exports.getCards = asyncHandler(async (req, res, next) => {
const cards = await Card.find({}).populate('user').exec();
res.status(200).json({
success: true,
count: cards.length,
data: cards,
});
});
它确实返回卡,但没有任何用户字段。
用户模式导出为“user”如果在引用用户集合时定义模型时犯了一个小错误,请删除单引号 模型定义应如下所示
const CardSchema = new mongoose.Schema({
text: {
type: String,
},
wrap: {
type: String,
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: User, // Replace 'User' with User
}
});
module.exports = mongoose.model('Card', CardSchema);