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

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

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

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 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