Node.js , multer, express, sequelize, Postgres
Node.js , multer, express, sequelize, Postgres
我目前正在尝试将图像存储在我的数据库中。
这是我的 post 模型:-
id: {
field: "PostId",
type: Sequelize.INTEGER,
primaryKey: true,
},
title: {
type: Sequelize.STRING,
},
images: {
type: Sequelize.STRING
}
这是我的 post 请求:-
router.post(
"/",
[
upload.array("images", config.get("maxImageCount")),
imageResize,
],
async (req, res) => {
const paths = await req.files.map((file) => ({ fileName: file.filename }));
Posts.create({
title: req.body.title,
images: JSON.stringify(paths),
}).then(
() => {
res.status(201).send({
msg: "upload successful",
});
},
(validation) => {
res.status(422).json({
errors: validation.errors.map((error) => {
return {
fieldName: error.path,
message: error.message,
};
}),
});});});
我现在正在使用 multer 将我的图像存储在磁盘上,并使用 sharp 来调整我的图像大小。
我的问题是当我去 localhost/posts 图片是这样的:-
`"images":"[{\"fileName\":\"95aa17.png-2020-11-08T14:24:33.221Z\"},{\"fileName\":\"fwfe.png-2020-11-08T14:24:33.268Z\"}]"`
当它应该看起来像这样时:-
`"images":"[{"fileName":"95aa17.png-2020-11-08T14:24:33.221Z"},{"fileName":"fwfe.png-2020-11-08T14:24:33.268Z"}]"`
经过几个小时的研究,我发现为了最佳实践,我不应该在数组上使用 JSON.stringify。
我该如何解决这个问题?
我设法解决了这个问题。这是解决方案:
router.post(
"/",
[
upload.array("images", config.get("maxImageCount")),
imageResize,
],
async (req, res) => {
const paths = await req.files.map((file) => ({ fileName: file.filename }));
Posts.create({
title: req.body.title,
images: paths.map((x) => ({ images: x.fileName }));
}).then(
() => {
res.status(201).send({
msg: "upload successful",
});
},
(validation) => {
res.status(422).json({
errors: validation.errors.map((error) => {
return {
fieldName: error.path,
message: error.message,
};
}),
});});});
我将图像作为文件名存储在数据库中。
我目前正在尝试将图像存储在我的数据库中。
这是我的 post 模型:-
id: {
field: "PostId",
type: Sequelize.INTEGER,
primaryKey: true,
},
title: {
type: Sequelize.STRING,
},
images: {
type: Sequelize.STRING
}
这是我的 post 请求:-
router.post(
"/",
[
upload.array("images", config.get("maxImageCount")),
imageResize,
],
async (req, res) => {
const paths = await req.files.map((file) => ({ fileName: file.filename }));
Posts.create({
title: req.body.title,
images: JSON.stringify(paths),
}).then(
() => {
res.status(201).send({
msg: "upload successful",
});
},
(validation) => {
res.status(422).json({
errors: validation.errors.map((error) => {
return {
fieldName: error.path,
message: error.message,
};
}),
});});});
我现在正在使用 multer 将我的图像存储在磁盘上,并使用 sharp 来调整我的图像大小。
我的问题是当我去 localhost/posts 图片是这样的:-
`"images":"[{\"fileName\":\"95aa17.png-2020-11-08T14:24:33.221Z\"},{\"fileName\":\"fwfe.png-2020-11-08T14:24:33.268Z\"}]"`
当它应该看起来像这样时:-
`"images":"[{"fileName":"95aa17.png-2020-11-08T14:24:33.221Z"},{"fileName":"fwfe.png-2020-11-08T14:24:33.268Z"}]"`
经过几个小时的研究,我发现为了最佳实践,我不应该在数组上使用 JSON.stringify。
我该如何解决这个问题?
我设法解决了这个问题。这是解决方案:
router.post(
"/",
[
upload.array("images", config.get("maxImageCount")),
imageResize,
],
async (req, res) => {
const paths = await req.files.map((file) => ({ fileName: file.filename }));
Posts.create({
title: req.body.title,
images: paths.map((x) => ({ images: x.fileName }));
}).then(
() => {
res.status(201).send({
msg: "upload successful",
});
},
(validation) => {
res.status(422).json({
errors: validation.errors.map((error) => {
return {
fieldName: error.path,
message: error.message,
};
}),
});});});
我将图像作为文件名存储在数据库中。