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

251
Visualizações
Cast to string failed for value Error when submitting the form, using FileBase from React to convert the image

I have an error when i try to post this schema data into Form.js. Mongoose schema looks like this:

const postSchema = mongoose.Schema({
    title: String,
    message: String,
    creator: String,
    tags: [String],
    selectedFile: String,
    likeCount: {
        type: Number,
        default: 0
    },
    createdAt: {
        type: Date,
        default: new Date()
    },
});

Logic for creating post is like this:

export const createPost = async (req, res) => {
    const post = req.body;

    const newPost = new PostMessage(post);

    try {
        await newPost.save();

        res.status(201).json(newPost);
    } catch(error){
        res.status(409).json({ message: error.message });
    }
}

I'm uploading image as String and I'm converting image using base64 from React like this-

import FileBase from 'react-file-base64';
<div className={classes.fileInput}><FileBase type="file" multiple="false" onDone={(base64) => setPostData({ ...postData, selectedFile: base64})}/></div>

When I try to submit form, I get error like - {"message":"PostMessage validation failed: selectedFile: Cast to string failed for value

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Found the answer, I was missing {} brackets aroung hte base64 after the onDone. so it should be like:

<div className={classes.fileInput}><FileBase type="file" multiple={false} onDone={({ base64 }) => setPostData({ ...postData, selectedFile: base64 })} /></div>

about 4 years ago · Santiago Trujillo 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