-1

I´m using typescript on node js to extracta data from Excel and save it on mongodb, there is however a field that has an id hardcoded, but when I try to save it as an ObjectId, I get the error:

 Argument passed in must be a single String of 12 bytes or a string of 24 hex characters

On this site I´ve seen several posts, and... the string doesnt have empty spaces and yes, I have imported the library

import { Types } from 'mongoose';
institucion: Types.ObjectId('5fe0c8dc7d8f144e8532252'),

The Schema, none of them work:

institucion: {type: Schema.Types.ObjectId},
institucion: {type: Object},

Saving it:

const guardarEmpresario = async (data: any, idEmpresa:any) => {
    data.negocio.push(idEmpresa);    
    //console.log('data.negocio ', data.negocio);
    
    var datoDelEmpresario: any = [];
    datoDelEmpresario.push({
        nombre: data.nombre,
        institucion: Types.ObjectId(data.institucion),
        correoElectronico: data.correoElectronico,
        telefono: data.telefono,        
        negocio: data.negocio
    });
    const empresarios = mongoose.model('Empresarios', EmpresariosSchema, 'Empresarios');
    return await empresarios.create(datoDelEmpresario);
Jhon Hernández
  • 293
  • 5
  • 20

1 Answers1

-1

After a while, I found that the id that my boss gave me was 1 character short.

Jhon Hernández
  • 293
  • 5
  • 20