我无法用节点 js 快速文件上传器中的新图像更新我的图像,我无法弄清楚

I can't get my image to be updated with the new one in node js express file uploader, I couldn't figure it out

我正在尝试更新我的 post 并且除图像外一切正常,当我更新图像 src 变得未知时,这是我的代码:

exports.updatePost = async (req, res) => {
  try {
    const post = await Post.findOne({ slug: req.params.slug });
    let uploadeImage = req.files.image;
    let uploadPath = __dirname + "/../public/uploads/" + uploadeImage.name;
    uploadeImage.mv(uploadPath, async () => {
      post.image = "/uploads/" + uploadeImage.name;
      post.title = req.body.title;
      post.desc = req.body.desc;
      post.image = req.body.uploadeImage;
      post.save();
      res.status(200).redirect("/posts");
    });
  } catch (error) {
    res.status(400).json({
      status: "fail",
      error,
    });
  }
};

最好使用multer作为你上传文件路径前的中间件 然后在您的路线中您可以访问 req.file ,将数据库中的参考存储为:

post.image= req.file.filename // or full path