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

159
Visualizações
How to just check if the mongodb .find returns something or not so true or false

How to find out if the .find returns something or not? I'm trying to make it so if it returns nothing, it saves a schema with an auth token (check 2), and if it returns something it does check 3. But everytime, even when I do not have a schema with my discordId in the database it logs check 3 and check 1 but never check 2.

let discordId = message.author.id;
const discordIdCheck = authSchema.find({
  discordId: discordId,
});
console.log('check 1');
if (discordIdCheck.count() < 1) {
  function getAuthToken() {
    var letters =
      '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!.#&';
    var authTokenStart = '';
    for (var i = 0; i < 12; i++) {
      authTokenStart += letters[Math.floor(Math.random() * letters.length)];
    }
    return authTokenStart;
  }

  const authToken = getAuthToken();

  const auth = {
    discordId: discordId,
    authToken: authToken,
  };
  console.log('check 2');
  message.author.send('Okay, your auth token is: ' + authToken);
  console.log(authToken);
  new authSchema(auth).save();
} else {
  message.channel.send(
    'You already have an auth token. use !token to make me send it to you in your direct messages',
  );
  console.log('Check 3 ');
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use the length value to check the length of the find() result like:

const discordIdCheck = await authSchema.find({
  discordId: discordId,
});

if(discordIdCheck.length <= 0) console.log("0 found")
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