Error passing JSON arguments

Asked

Viewed 70 times

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?

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

  • Note that in this classmodels[index] You are rewriting the entire record and not only the .id, same thing with the ObjectId(item), should be ObjectId(item.id)

2 answers

1


Through the baseball bat that rolled in the comments of the question we detected that only one was missing .id =)

//classmodels.forEach((item, index) => classmodels[index] = ObjectId(item));
classmodels.forEach((item, index) => classmodels[index].id = ObjectId(item.id));

0

Henry, look at this Mongodb documentation on Objectid: official documentation link: Mongo DB documentation

var Db = require('mongodb').Db,
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ReplSetServers = require('mongodb').ReplSetServers,
    ObjectID = require('mongodb').ObjectID,
    Binary = require('mongodb').Binary,
    GridStore = require('mongodb').GridStore,
    Grid = require('mongodb').Grid,
    Code = require('mongodb').Code,
    BSON = require('mongodb').pure().BSON,
    assert = require('assert');

// Create a new ObjectID
var objectId = new ObjectID();
// Convert the object id to a hex string
var originalHex = objectId.toHexString();
// Create a new ObjectID using the createFromHexString function
var newObjectId = new ObjectID.createFromHexString(originalHex)
// Convert the new ObjectID back into a hex string using the toHexString function
var newHex = newObjectId.toHexString();
// Compare the two hex strings
assert.equal(originalHex, newHex);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.