Error passing arguments in JSON

1

I'm trying to make an insertion in MongoDB by 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}
    ]
}

You are 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 able to use other endpoints with no problem at all.

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 to be called.

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));
        });
    },
    
asked by anonymous 19.10.2018 / 20:42

2 answers

1

Through the ball ball that rolled in the comments of the question we detected that only one .id =)

//classmodels.forEach((item, index) => classmodels[index] = ObjectId(item));
classmodels.forEach((item, index) => classmodels[index].id = ObjectId(item.id));
    
19.10.2018 / 22:34
0

Henry, look at this MongoDB documentation on ObjectID: link to the official documentation: 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);
    
19.10.2018 / 21:18