javascript - 猫鼬不居住
问题描述
我正在尝试填充我的一个模型,但它不起作用。这是我的卡片架构:
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,
});
});
它确实返回卡片,但没有任何用户字段。用户架构导出为“用户”
解决方案
您在引用用户集合时定义模型时犯了一个小错误,请删除单引号
模型定义应如下
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);
推荐阅读
- node.js - npm 更新:从 github repo 中提取不接受最新提交
- python - JSON 文件 Discord 中的表情符号解码错误,py
- algolia - 是否可以使用 Algolia 进行用户界面 A/B 测试?
- python - 是否可以在我的问题上应用 sklearn 评估指标,例如精度、召回率、f1_score?
- c++ - 将 0 存储在无符号字符数组中
- python - Python Sharepoint 库 Office365-REST-Python-Client 错误:401:未经授权的 url:https://mydomain.sharepoint.com/sites/sample/_api/Web
- c# - Azure Pipeline:dotnet 发布失败 - assets.json 没有“.NETCoreApp,Version=v3.1/win-x64”的目标
- c - 如何在 C 中查找错误
- ruby-on-rails - Simpleform Append 仅适用于某些输入类型
- bouncycastle - java - 如何使用现有的公钥进行 PGP 加密?