1
I am trying to make an insertion in Mongodb passing some arguments in JSON:
{
"name": "TESTE",
"category": "B",
"service": "Novo processo",
"description": "Novo Teste",
"active": false,
"classmodels": [
{"id": "5bbe37ef8eb26d0025624fcd", "amount": 10},
{"id": "5bbe37ef8eb26d0025624fcd", "amount": 14}
]
}
Is returning the following error:
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
It is also returning the status code as 500 (Internal Server Error), but I am managing to use other endpoints without any problem.
The functions in Nodejs that I’m using are these:
First to be called.
addStandardClassTemplate: function(req, res) {
let name = req.body.name;
let category = req.body.category;
let service = req.body.service;
let description = req.body.description;
let active = req.body.active;
let classmodels = req.body.classmodels;
console.log('Entrou no método de Adicionar Template de Aula Padrão');
classtemplatesMethods.add.addStandard(name, category, service, description, active, classmodels)
.then((result) => {
res.status(200).send(result);
})
.catch((error) => {
res.status(404).send(error);
});
},
Second call.
addStandard: function(name, category, service, description, active, classmodels) {
classmodels.forEach((item, index) => classmodels[index] = ObjectId(item));
let NewValues = {
name: name,
category: category,
service: service,
description: description,
active: active,
classmodels: classmodels,
};
console.log('QUERY Adicionar Template de Aula Padrão');
return new Promise((resolve, reject) => {
mongoMethods.add.insert(targetcollection, NewValues)
.then((result) => resolve(result))
.catch((error) => reject(error));
});
},
Hello Henrique, I saw that in your model you have this line: classmodels.foreach((item, index) => classmodels[index] = Objectid(item)); it is really necessary to re-populate the classmodels array with the same values?
– Adjair Costa
I am using this foreach to transform the id I pass to Objectid. I couldn’t understand what you said about re-populating the array.
– enriq
Note that in this
classmodels[index]
You are rewriting the entire record and not only the.id
, same thing with theObjectId(item)
, should beObjectId(item.id)
– Icaro Martins