Skip to content
Advertisement

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:

JavaScript

Logic for creating post is like this:

JavaScript

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

JavaScript

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

Advertisement

Answer

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>

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement