当我尝试在没有图像的情况下保存时,它显示以下错误“”消息”:“无法读取未定义的属性‘路径’”。?”

阿卜杜

当我尝试在没有图像的情况下保存时,它显示以下错误“”消息”:“无法读取未定义的属性‘路径’”。?”

    // 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档