1

problem

the value of affiliateXp dont show outside of the .foreach

code:

//get affiliateXp
let affiliateXp = 0

let invitedUsers = await userSchema.find({
    invitedById: userID
})

invitedUsers.forEach(async user => {

    let userXp = await getXpPerUser(user.discordId)
    affiliateXp = await affiliateXp + 1000 + userXp.totalXp*0.1
    console.log ('inside', affiliateXp)
        
});

console.log ('outside', affiliateXp)

console:

outside 0

outside 0

inside 1000.1073925

outside 0

inside 2000.1073925

outside 0

inside 3002.3100682

outside 0

inside 1000

Reza Rahemtola
  • 1,182
  • 7
  • 16
  • 30

1 Answers1

0

try using a global variable by declaring: var affiliateXp = 0 instead of let affiliateXp = 0