无法读取未定义的属性(读取' path') - multer错误
我正在通过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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我将其更改为 req.files 时,它起作用了。
It worked when I changed it to req.files