-1

I am trying to use the await parameters to make my functionality async. This works for the most part, other than the actual returned value is undefined in the function the async function is being called.

Here is the code I am using:

async function ResetPasswords(){
  //this will loop through all of the emails / usernames in the list and get the actual user instance and send the emails to them
  for (let i = 0; i < resetPasswordList.length; i++) {
    var Email = resetPasswordList[i];
    console.log("email: "+ Email);
    var userInstance = await getUser(Email);
    console.log("userInstance2");
    console.log(userInstance); *// userInstance is always undefined when the user hasn't been cached yet*
    if (userInstance != undefined){
      console.log("user instance: "+ Email + " is valid");
      console.log(userInstance);
      //todo: send email

      resetPasswordList.splice(i); //remove the email from the list once we send the email
    }else{
      console.log("userInstance is undefined...");
    }
}
}

async function getUser(EmailOrUsername){
  for (let i = 0; i < userList.length; i++) {
    var currentUser = userList[i];
    console.log(currentUser);
    if (currentUser.email == EmailOrUsername || currentUser.username == EmailOrUsername){
      return currentUser;
    }
  }
  //get the user's API key..
  console.log("starting the request");
  var JsonEmail = {
    email: EmailOrUsername
  }
  var JsonAPIKey = { }
  console.log("! begin getUserKey");
  await request("api/getUserKey", JSON.stringify(JsonEmail)).then(response => {
    var apiKey = response.apiKey;
    JsonAPIKey = {
      apiKey: apiKey
    }
    console.log("api key is: "+apiKey);
    if (apiKey == "undefined"){
      console.log("API KEY IS UNDEFINED");
      return undefined;
    }
    
});
console.log("! begin getUser");
await request("api/getUser", JSON.stringify(JsonAPIKey)).then(response => {
var userData = response.userData;
userData.teams = response.teamsData; //add the teams data into the user's data obj...
userList.push(userData); // add the user instance to the array for caching...
console.log(userData); *//THIS LINE ACTUALLY PRINTS THE CORRECT DATA OBJECT...*
console.log("FINISHED GET USER");
return userData;
});
}

So in the function "ResetPasswords", is where I am calling the function "getUser()". Essentially, when in the getUser function, at the end where the line is "console.log(userData);" it will actually print out the JSON object I want. But then in the "ResetPasswords" function that same returned object is undefined. This would make sense if I'm logging the output from the getUser function before it actually completes, but I am not. Below is the order of operation:

email: tt
starting the request

! begin getUserKey
274ms elapsed
api key is: uyEBOsMmeTcPr21

! begin getUser
238ms elapsed
user data: [object Object]
{email: 'tt', username: 't', verified: false, verificationCode: null, …}

FINISHED GET USER
userInstance2
undefined
userInstance is undefined...

Am I doing something wrong with the "await" parameter?

Thanks in advanced!

Trent
  • 11
  • 4

0 Answers0