Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda