node.js - 当我尝试在没有图像的情况下保存时,它显示以下错误““消息”:“无法读取未定义的属性'路径'”。?
问题描述
当我尝试在没有图像的情况下保存时,它显示以下错误““消息”:“无法读取未定义的属性'路径'”。?
// product controller
exports.products_create_product = (req, res, next) => {
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price,
productImage: req.file.path
});
product
.save()
.then(result => {
console.log(result);
res.status(201).json({
message: "Created product successfully",
createdProduct: {
name: result.name,
price: result.price,
_id: result._id,
request: {
type: "GET",
url: "http://localhost:3000/products/" + result._id
}
}
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
};
// 以下是我的产品路线
router.post("/", checkAuth, upload.single('productImage'),
ProductsController.products_create_product);
解决方案
使检查像
req.file? req.file.path : null
推荐阅读
- google-apps-script - 修改代码 - 将永久抄送添加到 MailMerge - Google 表格/Google Apps 脚本
- python - 如何使用 SpaCy 解决西班牙语词形还原问题?
- java - 代码在 Java 递归中返回堆栈溢出错误
- asp.net-mvc - IdentityServer4 客户端应用程序:ArgumentNullException:IDX10000:参数“hashAlgorithm”不能是“null”或空对象
- android - 设置firebase时出错,可能会出现未处理的承诺拒绝
- linux - Poco::Net::HTTPSClientSession receiveResponse 总是被异常截断
- ios - 删除、添加或修改 Firestore 文档和分页结果后重新加载 TableView
- c++ - 为什么 unique_ptr 和 shared_ptr 不会使构造它们的指针无效?
- python - 如何改变 wxpython 的方向?
- javascript - TypeError: <...> 不是函数