最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

运维笔记admin10浏览0评论

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

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

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

    // 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
发布评论

评论列表(0)

  1. 暂无评论