无法读取未定义的属性(读取' path') - multer错误

发布于 2025-01-29 01:51:20 字数 1778 浏览 1 评论 0原文

我正在通过Multer软件包将图像上传到文件夹中。在测试Postman中的路线时,我会遇到错误。

location_model.js

const util = require("util");

const locationSchema = mongoose.Schema(
  {
    _id: mongoose.Schema.Types.ObjectId,
    name: String,
    latitude: { type: Number, required: true },
    longitude: { type: Number, required: true },
    heightmapImage: { type: String, required: true },
  },
  { bufferCommands: false } //no biffer time limit
);

module.exports = mongoose.model("Location", locationSchema);

这是我位置路由器的代码,同时发布了图像的新位置

router.post("/", upload.single("heightmapImage"), (req, res, next) => {
  //Model
  console.log(req.file);
  console.log(req.file.path);
  const location = new Location({
    _id: new mongoose.Types.ObjectId(),
    name: req.body.name,
    latitude: req.body.latitude,
    longitude: req.body.longitude,
    heightmapImage: req.file.path
      .replace(/\\/g, "/")
      .substring("public".length),
  });
  location
    .save()
    .then((result) => {
      //save method provided by mongoose, will save in the databas
      console.log(result);
      res.status(201).json({
        //message: "Working POST",
        message: "Created Location successfully",
        createdLocation: {
          name: result.name,
          latitude: result.latitude,
          longitude: result.longitude,
          request: {
            type: "GET",
            url: "http://localhost:3000/location/" + result._id,
          },
        },
      });
    })
    .catch((err) => {
      console.log(err);
      res.status(500).json({ error: err });
    });
});

,这是在Postman中进行发布时的错误 -

{
    "error": {
        "message": "Cannot read properties of undefined (reading 'path')"
    }
}

I am uploading an image to a folder in uploads via multer package. I am getting an error while testing the route in POSTMAN.

location_model.js

const util = require("util");

const locationSchema = mongoose.Schema(
  {
    _id: mongoose.Schema.Types.ObjectId,
    name: String,
    latitude: { type: Number, required: true },
    longitude: { type: Number, required: true },
    heightmapImage: { type: String, required: true },
  },
  { bufferCommands: false } //no biffer time limit
);

module.exports = mongoose.model("Location", locationSchema);

This is the code for my location router while posting new location with image

router.post("/", upload.single("heightmapImage"), (req, res, next) => {
  //Model
  console.log(req.file);
  console.log(req.file.path);
  const location = new Location({
    _id: new mongoose.Types.ObjectId(),
    name: req.body.name,
    latitude: req.body.latitude,
    longitude: req.body.longitude,
    heightmapImage: req.file.path
      .replace(/\\/g, "/")
      .substring("public".length),
  });
  location
    .save()
    .then((result) => {
      //save method provided by mongoose, will save in the databas
      console.log(result);
      res.status(201).json({
        //message: "Working POST",
        message: "Created Location successfully",
        createdLocation: {
          name: result.name,
          latitude: result.latitude,
          longitude: result.longitude,
          request: {
            type: "GET",
            url: "http://localhost:3000/location/" + result._id,
          },
        },
      });
    })
    .catch((err) => {
      console.log(err);
      res.status(500).json({ error: err });
    });
});

And this is the error while doing POST in Postman -

{
    "error": {
        "message": "Cannot read properties of undefined (reading 'path')"
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も让我眼熟你 2025-02-05 01:51:20

当我将其更改为 req.files 时,它起作用了。

It worked when I changed it to req.files

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文