Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
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);
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda