我正在尝试向数据库发出发布请求,图像的数据类型是一个字符串,我可以上传一个、两个或多个图像。
我应该如何存储图像?
这是我的帖子模型
const Sequelize = require("sequelize");
const db = require("../config/database");
const Posts = db.define(
"post",
{
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: 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: 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,
};
}),
});
}
);
}
);
为什么我老是报错???{
"errors": [
{
"fieldName": "images",
"message": "images cannot be an array or an object"
}
]
}
最佳答案
您的代码定义了 const paths = ...
,但随后您的 Posts.create()
使用没有“S”的 path
。我认为这是您问题中的一个错字,因为 req.files.map()
会生成一个数组。您不能将数组插入到列值中,这是错误告诉您的。
关于node.js - 图像不能是数组或对象 - sequelize、postgres、nodejs、multer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64753687/