node.js - Mongoose:尝试将嵌套对象字段设置为原始值“null”
问题描述
我有一个带有嵌套字段的架构,例如
// model.js
module.exports = Mongoose.model('Model', new Mongoose.Schema(
{
Top: {
Middle: {
Bottom: Number
}
}
},
{
strict: 'throw'
})
)
所有这些字段都存在是非常罕见的,当一个字段不存在时,生成它的进程会写入null
. 我想将严格模式设置为,true
但它似乎不喜欢应该包含 Object 的字段而不是包含null
:
// app.js
const Model = require('./model');
router.post('/api' , async (req, res) => {
const doc = new Model(req.body);
const result = await doc.save();
res.json(result);
})
// test
axios.post('/api', { Top: { Middle: null }})
=> ObjectExpectedError: Tried to set nested object field 'Middle' to primitive value 'null' and strict mode is set to throw.
有没有办法规避这种行为?不幸的是,让生成数据的过程undefined
代替null
.
解决方案
推荐阅读
- c - 在 Eclipse 中对整个 C 项目运行自动格式化程序
- amazon-web-services - 如何在python中处理aws Lambda重试
- android - 多个 AutoCompleteTextView 的 OnItemClickListener 与 switch case 不工作
- php - MySQL select where ID is IN (SELECT another table)
- mfc - 如何通过 cppunit 测试 mfc 对话框项目
- python - Minimax 算法不适用于 4x4 TicTacToe
- apache-spark - 火花调整问题
- xml - XML Schema 验证针对相同的错误抛出两个不同的错误消息
- mysql - 在 MYSQL 查询中使用 TOP 删除重复项会引发错误代码
- sql - 基于一个字段的不同记录在基于rails4中的另一个字段分组后没有列出