javascript - 如何在猫鼬/节点 JS 中解决这个引用问题
问题描述
我有用户的路线和模型,然后是贷款的另一个。我试图在 Loan 路由中引用用户,但每当我在 PostMan 上测试时都会收到此错误:
TypeError: Cannot read property '_id' of undefined
at C:\Users\Micho\Documents\GBENGA\BE\src\routes\loans\index.js:38:47
贷款模型代码为:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const loanSchema = new Schema({
customerName: {
type: String,
required: true
},
gender: {
type: String
},
address: {
city: String,
state: String,
},
amount: {
type: Number
},
loanTenure: {
type: Number
},
user: {
type: Schema.Types.ObjectId,
ref: 'User'
},
loanStatus: {
type: String,
default: "created"
}
}, {
timestamps: true
})
我的路线是这样的:
router.post("/", async (req, res) => {
try {
let loan = await new Loan({...req.body});
loan.save();
await User.findByIdAndUpdate(req.user._id, { $push: { loans: loan._id } })
console.log(req.user)
loan = await Loan.findById(loan._id).populate("user");
res.send(loan);
} catch (error) {
console.log(error)
res.status(500).send(error);
}
});
请帮忙。谢谢
解决方案
推荐阅读
- django - ModelForm 显示下拉菜单而不是新表单
- kubernetes - 重新创建目标 pod 后主机内 UDP 流量丢失
- android - 如何按照选择的顺序将我的意图中选择的照片添加到我的适配器?
- authentication - ASP.NET Core 身份验证:同一控制器中一个操作的承载令牌和另一个操作的 Windows 身份验证
- python-3.x - 停用词将负面评论变为正面评论。在文本摘要过程中删除停用词的好方法是什么?
- c# - Unity3D 中的 C# 脚本仅 debug.log 而不是旋转
- javascript - 如何在本机反应中以 ISO 格式转换 dd/mm/yyyy
- c# - 如何导入 dll 函数 (DllInstall()) 以便能够使用 regsvr32 进行注册?
- java - 图库中的多个图像转换为 PDF
- postgresql - AWS Terraform postgresql 提供程序:服务器上未启用 SSL