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

136
Visualizações
reads value brefore for loop js

i want to return value after the for loop but for some reason reads the value brefore the loop

client.on('friendMessage', (steamID, message) => {
  var cmd;
  if(cmd = message.match(/^!amount (\D+)/)) {
    var item_idz = cmd[1];
    console.log(amnt(item_idz))
  }
});
function amnt(item_idz,steamID) {
  manager.getUserInventoryContents("[U:1:1227885041]", 440, 2, true, (err, inventory) => {
    if(err) {
      console.log("coś sie zepsuć");
    } else {
      var ew = 0;
      for(var p = 0; p< inventory.length; p++) {
        if(inventory[p].name == item_idz) {
          ew++;
          console.log(ew)
        }
      }
      if(steamID != null) {
        client.chatMessage(steamID, ew.toString());
      } else {
        return ew;
      }
    }
  });
}

output: 7656119884397**** said !amount Crimson Cache Case undefined 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This console.log show undefined Because you use callback and amnt append in javascript event stack until the process has been finish

On other hand function amnt return nothing cause you execute callback function and return nothing

You try this code for print data:

if(cmd = message.match(/^!amount (\D+)/)) {
  var item_idz = cmd[1];
  amnt(item_idz, (error, data) => console.log(data));
}

function amnt(item_idz, steamID, next) {
  manager.getUserInventoryContents('[U:1:1227885041]', 440, 2, true, (err, inventory) => {
    if (err) {
      console.log('coś sie zepsuć');
    } else {
      var ew = 0;
      for (var p = 0; p < inventory.length; p++) {
        if (inventory[p].name == item_idz) {
          ew++;
          console.log(ew);
        }
      }
      if (steamID != null) {
        client.chatMessage(steamID, ew.toString());
        next(null);
      } else {
        next(null, ew);
      }
    }
  });
}
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