【问题标题】:Can not read property "Buffer" of "undefined"无法读取“未定义”的属性“缓冲区”
【发布时间】:2019-04-02 11:01:29
【问题描述】:

我正在构建一个应用程序,在该应用程序中填写一个也有文件的表单。我从“req.file”获取文件,从“req.body”获取其他内容。 “文件”不是“必需的”。用户可以附加或不能附加。当用户附加时,一切顺利,但是当用户不附加文件时,我发现了这个错误。 can not read property "buffer" of undefined.。我已将“图像”的猫鼬模式类型设置为“缓冲区”。这是我从“req”获取文件和其他数据时的代码。

image: req.file.buffer,

geolocation: req.body.geolocation,

details: req.body.details,

location: req.body.location,

status: req.body.status,

spam: req.body.spam,

【问题讨论】:

  • 在将req.file.buffer 分配给图像之前检查req.file

标签: node.js mongodb file mongoose buffer


【解决方案1】:

假设您是在将对象添加到 db 之前创建对象,您需要先检查 req.file 是否为空。

{
 image: req.file.buffer ?  req.file.buffer :  null; 
 geolocation: req.body.geolocation,
 details: req.body.details,
 location: req.body.location,
 status: req.body.status,
 spam: req.body.spam
}


因此,如果您的表单没有 req.file.buffer,它不会引发错误。

【讨论】:

  • 我设法解决了。它应该是 ' ' 而不是 null。我试过“”。也许您的案例“空”有效。但非常感谢。 :)
猜你喜欢
  • 2021-07-30
  • 2019-01-20
  • 1970-01-01
  • 1970-01-01
  • 2021-03-15
  • 2021-05-10
  • 2015-03-27
  • 2021-02-27
  • 2019-11-26
相关资源
最近更新 更多