7- Project- Add Persistence to Genres API

Hi

The post method does not work for me.
The problem is with the _id : when I send :
{
“_id” : “3”,
“name” : “Sci-fi”
}
from postman I get error message as response:
“_id” is not allowed

But even when I don’t use the id so I send from postman:
{
“name” : “Sci-fi”
}
I get error on console : document must have an _id before saving.

I’m using Atlas cloud from MongoDB and not local, I think this might be the cause ? So if I want to insert an Id myself I simply can’t ? Please help.

Here is my method:
router.post(’/’,async function createGenre(req,res) {

const { error } = validateGenre(req.body);

if (error) return res.status(400).send(error.details[0].message);

const genre = new Genre ({

    //_id: req.body.id,

    name: req.body.name

});



try {

    const result = await genre.save();

    res.send(result);

}

    catch (ex) {

        console.log(ex.message);

}

});

Thanks in advance.

Okay I found the solution. I must not have _id in the object Schema so that it is created auto by mongo. I was having:

const genreSchema = new mongoose.Schema({
//_id: String,
name:{
type: String,
required: true,
}
});

So I tried to force an ID by myself but it simply does not let me which is a bit annoying :D.
On compass:
Screenshot_12
which looks ugly. The first were created from the code the others using postman.

I’m having problems with put method aswell:

I tried to update documents with _id : 1 and also with a auto-generated _id like: 5ff5d5340a89b23afc72b865

My code is similar to the one in the video.

Please help !

Okay I fixed it . Seems like I was parsing the id to int and I forgot to delete that.