node.js - 如何在更新猫鼬上设置 _id 引用
问题描述
这是我的模型。
const mongoose = require('mongoose');
const schema = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
brandName: {
type: String,
required: true
},
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
})
module.exports = mongoose.model('Food', schema);
我想将 userId 保存为食物集合的参考。
const newFood = new Food({
_id: new mongoose.Types.ObjectId,
brandName: req.body.brandName,
user = mongoose.Types.ObjectId(req.body.userId),
})
现在我收到了这个控制台错误。
user = mongoose.Types.ObjectId(req.body.userId),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Invalid shorthand property initializer
我不确定我做错了什么。
解决方案
你有一个错字,使用:
而不是=
在用户
user: mongoose.Types.ObjectId(req.body.userId)
推荐阅读
- java - 是否打算彻底检查现有的 Java 平台类(JEP 360)?
- javascript - AngularJS的ng-repeat in line,逗号分隔,两个属性在同一行
- dbt - 为正值编写 dbt 测试
- node.js - 使用 passport.js 注册后出现错误的请求错误
- angular - 添加 ngAfterViewChecked 后,scrollTop 不起作用
- spring-boot - SpringDoc (spring boot) utf-8 json 标头响应文档
- c# - 解析没有根元素的 XML 文档
- c# - 如果使用 dbfirst,实体框架会创建表或字段
- reactjs - 使用路由器 dom 在 React 中创建链接的问题
- python - 输出文件地址(Python)