Skip to content
Advertisement

Can’t upload picture to MongoDb from React frontend

I’m creating a tinder clone, I can create a user fine however I cant upload a picture. The error I get is ValidationError: User validation failed: pictures: Cast to embedded failed for value “‘picture'” at path “pictures”. I’m not sure what I’m doing wrong. The post request seems to fire as excepted with the payload however its when I login when I get the error. So I’m sure this has something to do with initial creation of the account.

create account front and back

JavaScript
JavaScript

mongoDb Schema

JavaScript

login backend and frontend

JavaScript
JavaScript

Advertisement

Answer

first of all you can’t upload image like this because you send regular http request if you want to send iamge you need to follow this steps

in the frontend you need to send the request with form data for more info read this blog from mdn what is formData mdn you can do something like that with axios append all of the req body to the formData and add it to the axios add multipart/form-data header

JavaScript

in the server you need to upload-files-or-images-to-server-using-nodejs

Advertisement