Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
TypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters

Below is the code, ive also tried the $regex method, it only gives me the 'Cant use $regex' error.

let query : any;
        if (data.search.length > 0) {
            query = { _id: new RegExp(`^${data.search}.*`), accountId };
        } else {
            query = { accountId };
        }

        const orders: any = await TEMPOrders.find(query)

            .lean()
            .exec();
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can simply use ObjectId() for mongoose _id field for querying and the search value should also be an ObjectId,otherwise it will throw an error of string must be of length 12 bytes or 24 hex characters.

As _id is the primary key and it defines each documents uniquely in the collection. So ObjectId is used for comparison or for querying _id.

Import ObjectId as shown below:

var ObjectId = require('mongodb').ObjectId 

query = { _id: ObjectId(data.search), accountId };
about 4 years ago · Juan Pablo Isaza Denunciar

0

To avoid this error must convert to objectID and also add term()

var ObjectId = require(mongodb).ObjectId

var id = (req.body.id).term();
db.col.findOne({'_id':ObjectId(id)},(err,doc)=>{
 if(err){
   console.log(err)
 }else{
   console.log(doc)
 }
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda