Multer Error: Image url is not save in my Database
Multer Error: Image url is not save in my Database
起初我遇到了Multer: Unexpected field error,后来我修复到这里。除图像外,每个板列都保存在数据库中。是我的邮递员测试方法错了吗?或者我的代码有问题?如何在图板图像列中保存图像 url(路径)?
const upload = multer({
storage: multer.diskStorage({
destination(req, file, done) {
done(null, "uploads/");
},
filename(req, file, done) {
const ext = path.extname(file.originalname);
const basename = path.basename(file.originalname, ext);
done(null, basename + new Date().getTime() + ext);
},
}),
limits: { fileSize: 5 * 1024 * 1024 }, // 5MB
});
router.post("/images", upload.single("image"), (req, res, next) => {
res.json({ url: `/uploads/${req.file.filename}` });
});
router.post("/", upload.single("image"), async (req, res, next) => {
try {
const {
category,
title,
description,
tags,
latitude,
longitude,
mainAddress,
detailAddress,
} = req.body;
const createBoards = await boards.create({
category,
title,
description,
tags,
latitude,
longitude,
mainAddress,
detailAddress,
image: req.body.url,
});
return res.status(200).json({ data: createBoards, message: "OK" });
} catch (err) {
return res.status(500).json({ message: "Error"});
}
});
enter image description here
enter image description here
enter image description here
如果 url
是指 multer 为您上传的文件分配的实际路径,您可以在 file
对象上使用 属性 path
。有关所有可用属性,请参阅 this。
所以在你的情况下你可以这样做:
const createBoards = await boards.create({
category,
title,
description,
tags,
latitude,
longitude,
mainAddress,
detailAddress,
image: req.file.path // to be safe you should maybe add a check if req.file is defined
});
起初我遇到了Multer: Unexpected field error,后来我修复到这里。除图像外,每个板列都保存在数据库中。是我的邮递员测试方法错了吗?或者我的代码有问题?如何在图板图像列中保存图像 url(路径)?
const upload = multer({
storage: multer.diskStorage({
destination(req, file, done) {
done(null, "uploads/");
},
filename(req, file, done) {
const ext = path.extname(file.originalname);
const basename = path.basename(file.originalname, ext);
done(null, basename + new Date().getTime() + ext);
},
}),
limits: { fileSize: 5 * 1024 * 1024 }, // 5MB
});
router.post("/images", upload.single("image"), (req, res, next) => {
res.json({ url: `/uploads/${req.file.filename}` });
});
router.post("/", upload.single("image"), async (req, res, next) => {
try {
const {
category,
title,
description,
tags,
latitude,
longitude,
mainAddress,
detailAddress,
} = req.body;
const createBoards = await boards.create({
category,
title,
description,
tags,
latitude,
longitude,
mainAddress,
detailAddress,
image: req.body.url,
});
return res.status(200).json({ data: createBoards, message: "OK" });
} catch (err) {
return res.status(500).json({ message: "Error"});
}
});
enter image description here
enter image description here
enter image description here
如果 url
是指 multer 为您上传的文件分配的实际路径,您可以在 file
对象上使用 属性 path
。有关所有可用属性,请参阅 this。
所以在你的情况下你可以这样做:
const createBoards = await boards.create({
category,
title,
description,
tags,
latitude,
longitude,
mainAddress,
detailAddress,
image: req.file.path // to be safe you should maybe add a check if req.file is defined
});