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

102
Vistas
How do I get my async function to work? Typescript

I'm working on a discord bot and need to check if a user is in my database or not so that I can make a profile for new users. I'm new at using async functions so I've been looking around for examples but can't seem to get it to work how I want.

Using Typescript / DiscordJS / mongoDB

let profileData;
try {
  const makeProfile = async () => {
    profileData = await profileModels.findOne({ userID: message.author.id });
    if (!profileData) {
      setTimeout(async () => {
        await new profileSchema({
          userID: message.author.id,
          serverID: message.guild?.id,
          balance: 0,
          inventory: [],
          bank: 0,
        }).save();
      }, 1000);
    }
  };
} catch (err) {
  console.log(err);
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

async function makeProfile() {
  try {
    const profileData = await profileModels.findOne({
      userID: message.author.id,
    });

    await new profileSchema({
      userID: message.author.id,
      serverID: message.guild?.id,
      balance: 0,
      inventory: [],
      bank: 0,
    }).save();
  } catch (error) {
    console.log(error);
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

let profileData; // Left this in global scope in case you want to access it outside the function scope
async function makeProfile() {
  try {
    profileData = await profileModels.findOne({ userID: message.author.id });
    if (!profileData) {
      setTimeout(() => {
        await new profileSchema({
          userID: message.author.id,
          serverID: message.guild?.id,
          balance: 0,
          inventory: [],
          bank: 0,
        }).save();
      }, 1000);
    }
  } catch (err) {
    console.log(err); // Would recommend console.error for errors
  }
}

Make sure to await the function when calling it if you care about the timing.


Since this is a discord bot and the function appears to be initialized inside the message_create listener, you might want to consider making a parameter for message and moving this out of the event listener.

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