'Image cannot be an array or an object

I am developing a project which consists of publishing a post and being able to upload a user image. I used JS for Front and NodeJs for Back (Multer for uploading images).I have an error for making a post and uploading an image to my DDBB in Mysql. I created the posts without any errors, but when I implement ability of sending an image I got this error:

string violation: image cannot be an array or an object

Code:

//Here call the action from PostActions
const addPost = (body) => dispatch(createPostAction(body));

const userId = props.credentials?.user.id

//Formate the picture
const formdata = new FormData();
formdata.append('image', image, userId);
const user = post.user;
const token = post.token;

//Create the post
addPost({
    title,
    content,
    image,
    userName: post.name,
    lastName: post.lastName,
    date: new Date(),
    userId: user.id,
    token: token,
});
//My route
router.post("/", authenticate, fileUpload, async(req, res) => {
    try {
        const id = req.body;
        const image = req.file;
        res.json(await postControllers.makePost(id, image))
    } catch (error) {
        return res.status(500).json({
           message: error.message
        });
    }
});
//My controller
async makePost(post) {
    return Post.create(post);
}


Solution 1:[1]

I don't recommend storing images in a mysql database, or any database in general.

The most common way to handle image uploads it to store an image into a file storage system like Amazon S3 or Cloudfront, and then store the image url you receive (which is usually the file path to the image, and is a string), in the database.

This is due to both performance and bandwidth optimization.

However, if you want to store it in your MySQL database, you need to store it as a blob. Ex:

CREATE TABLE 'test'.'pic' (
  'pic' INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  'img' LONGBLOB NOT NULL,
  PRIMARY KEY ('pic')
)

This string violation could be due to MySQL expecting a string (path to an image), and is not configured to receive a blob type.

To test, try passing in a string as the value of the image key, and see if adding the post succeeds.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1