Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!