Search code examples
javascriptnode.jsvariablesdiscordtrello

Get value of defined variable which is in forEach loop


var rank = "yeah";
var one, two, three, four;

Trello.board.searchCards('Myboardidhere').then(function(response) {
  for (let i = 0; i < response.length; i++) {
    const element = response[i];
    if (element.name === m.content) {
      var idList = element.idList;
      if (idList === sdLid) {
        rank = "Sheriff Deputy";
      } else if (idList === dfcLid) {
        rank = "Deputy First Class";
      } else if (idList === cplLid) {
        rank = "Corporal";
      } else if (idList === sgtLid) {
        rank = "Sergeant";
      } else if (idList === ltLid) {
        rank = "Lieutenant";
      } else if (idList === cptLid) {
        rank = "Captain";
      } else if (idList === devLid) {
        rank = "Developer";
      } else if (idList === mjrLid) {
        rank = "Major";
      } else if (idList === hdevLid) {
        rank = "Head Developer";
      } else if (idList === asLid) {
        rank = "Assistant Sheriff";
      } else if (idList === usLid) {
        rank = "Undersheriff";
      } else if (idList === SLid) {
        rank = "Sheriff";
      } else {
        rank = "Citizen";
      }
      var one = rank;
      rank = one;
      console.log(one + " - one");
    }
    var test = one;
    console.log(test + " - test");
  }
  var test2 = test;
  var anotherTest = 12;
  console.log(test2 + " - test2");
});
console.log(anotherTest);

console.log(rank + " - rank");

//CHANGE USERNAME
message.member.setNickname(m.content + " | " + rank) // Sets the users nickname

Hello. So I am currently working with node.js (discord JS trelloapi). Well, there's actually no need to exactly explain what I'm trying to do, but I am trying to get value of rank variable which is in for(;;) loop and in a block. (Have been searching a lot, couldn't find the answer)


Solution

  • make use of async and await. anotherTest is inside a promise, which is asynchronous. That means the value is only set after you console.log it. If you make use of await, then this code will act as if it is synchronous.