0

I have a function that should return an array of Object with 2 properties:

setusersExpiryDate() {

let userList = [];
let userList2 = [];

for (let i = 0; i < this.usersIdsList.length; i++) {
    this.userService.getUserDetails(this.usersIdsList[i]).subscribe(
        user => {
            if (user.type === 'INTERNAL') {
                userList[i] = {
                    user: this.usersIdsList[i],
                    expirationDate: this.getInternalExpiryDate()
                }
            }
            if (user.type === 'EXTERNAL') {
                userList[i] = {
                    user: this.usersIdsList[i],
                    expirationDate: this.getExternalExpiryDate()
                }
            }
        },
        error => console.log(error)
    );
    userList2[i] = {
        user: this.usersIdsList[i],
        expirationDate: this.getInternalExpiryDate()
    }
}
console.log(userList);
console.log(userList2);

return userList;
}

the first array userList will return something like this:

[]
 0: {user: 'USER222', expirationDate: Tue Oct 25 2022 12:32:45 GMT+0100 (Western European 
  Summer Time)}
 1: {user: 'USER321', expirationDate: Sun Oct 25 2026 12:32:45 GMT+0000 (Western European 
    Standard Time)}
 2: {user: 'USER011', expirationDate: Sun Oct 25 2026 12:32:45 GMT+0000 (Western European 
    Standard Time)}
 length: 3
 [[Prototype]]: Array(0)

and the 2nd array userList2 , which is outside of Observable is giving the right result:

(3) [{…}, {…}, {…}]
 0: {user: 'USER222', expirationDate: Mon Oct 25 2021 12:32:45 GMT+0100 (Western European 
     Summer Time)}
 1: {user: 'USER321', expirationDate: Mon Oct 25 2021 12:32:45 GMT+0100 (Western European 
     Summer Time)}
 2: {user: 'USER011', expirationDate: Mon Oct 25 2021 12:32:45 GMT+0100 (Western European 
     Summer Time)}
 length: 3
 [[Prototype]]: Array(0)

I want to know what I'm doing wrong with array userList , because I want it return same result as userList2.

userList => [] empty array : Wrong / userList2 => [{…}, {…}, {…}] array with 3 objects on it : right

Pedro Mendes
  • 99
  • 2
  • 12
  • You should probably include the implementation of `getExternalExpiryDate`. – Octavian Mărculescu Oct 25 '21 at 11:44
  • @OctavianMărculescu it is just a function that returns a new date + more years – Pedro Mendes Oct 25 '21 at 11:48
  • See also https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example – Heretic Monkey Oct 25 '21 at 11:50
  • @HereticMonkey i have changed those two array to global variables , but still getting same issue – Pedro Mendes Oct 25 '21 at 11:57
  • 1
    `geUserDetails()` is async. So in the moment you are doing `console.log(userList); return userList;` neither `getUserDetails` nor the callback of `subscribe` have been executed yet. Thus `userList` is not filled yet ... It doesn't matter whether it is global or not. Read the links provided by @HereticMonkey – derpirscher Oct 25 '21 at 12:08

0 Answers0