I am a project, and my goal is to create an API. During authentication a user can create a sauce with different fields. I am working with mongoDB. When I submit the form, the image saves well in my backend, but nothing on the page and nothing on collection (mongodb atlas).
This is what I have to respect :
request body :{ sauce: String,image: File }
type of response :{ message: String }Verb
And which is recorded from the frontend. I just have to take care of the backend and not touch anything on the frontend side.
I have successfully connected in my mongoose apps.js file, and I have no error on the connection. But as the form is not displayed, nothing is sent on mongodb atlas too.
I have this message : SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse ()
at exports.createThing .
exports.createThing = (req, res, next) => {
const thingObject = JSON.parse(req.body.thing);
delete thingObject._id;
const thing = new Thing({
...thingObject,
imageUrl: `${req.protocol}://${req.get("host")}/images/${
req.file.filename
}`,
});
thing
.save()
.then(() => res.status(201).json({ message: "
registered object !" }))
.catch((error) => res.status(400).json({ error }));
};