Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
Cómo verificar si mongodb .find devuelve algo o no es tan verdadero o falso

¿Cómo saber si el .find devuelve algo o no? Estoy tratando de hacer que si no devuelve nada, guarde un esquema con un token de autenticación (verifique 2), y si devuelve algo, verifique 3. Pero siempre, incluso cuando no tengo un esquema con mi discordId en la base de datos registra el check 3 y el check 1 pero nunca el 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 '); }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede usar el valor de length para verificar la longitud del resultado de find() como:

 const discordIdCheck = await authSchema.find({ discordId: discordId, }); if(discordIdCheck.length <= 0) console.log("0 found")
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!