我有一个猫鼬模式,它有一个comments: [String]
.
当我尝试更新架构时,我通过将以下代码添加到我的控制器中来检查值是 null 还是空,
if (req.body.comments !== null || req.body.comments !== '') { container.comments.push(req.body.comments) }
container.save() ...
但是,当我运行此代码(使用邮递员进行测试)并且没有为注释字段提供值时,它在数据库中更新为数组中的空值。
comments: [String]
位于主模式定义中,而不是 suc 文档或类似内容的一部分。
有没有人知道解决这个问题的方法或者为什么会发生这种情况?
确保注释不是undefined
,您可以通过以下方式处理所有情况:
if (req.body.comments && req.body.comments !== '') {
container.comments.push(req.body.comments)
}
这样就不会添加空字符串、空值和未定义值,假设req.body.comments
总是以字符串形式出现。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句