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
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>