1

When I call the method interval, the parameter user, which I get from the method allUserStats, is always undefined. Here is the method

const interval = async () => {
    const allUsers = await getAllUser();
    for (const u of allUsers) {
            let user;
            let followerScore;
            let subscriberScore;
            //let hostsScore;
            let viewerScore;
            await getAllUserStats(u.login_name).then(async (userStats)=>{
                user = userStats;
...

The method getAllUserStats looks like this:

const getAllUserStats = async (login_name) => {
    await userstats.findOne({login_name}).then(async(userStats)=>{
        if(userStats===null){
            let user = new userstats({
                login_name:login_name,
                followers: [],
                subscribers: [],
                hosts: [],
                viewers: []
            })
            await user.save();
            return user;
        }
        else{
            return userStats;
        }
    })
}

I think it is an async-await issue, but I dont know. Any advice how I can fix that?

  • Have you tried returning something from the `getAllUserStats` function? eg `return await userstats...` – badsyntax Jan 15 '21 at 13:03
  • Yeah, `getAllUserStats` doesn't return anything. You can either a) return a Promise/then-chain and `await` the function call or b) `await` the result, then actually return it. You are still trying to return from a callback function, which will simply discard the value. A working `return` has to be directly inside the function, it cannot be wrapped with a callback function. –  Jan 15 '21 at 13:06
  • @ChrisG thank you, now it works. Could you add your comment as an answer, so I can accept it and close the thread? – JackJackson12 Jan 15 '21 at 13:09
  • I've shown @ChrisG comment (B) option might look like. That's generally the best option -- if you're using async/await, go for it and use it! forget `.then` – TKoL Jan 15 '21 at 13:12
  • @TKoL This is exactly what I did now. Thanks a lot! – JackJackson12 Jan 15 '21 at 13:15
  • I help people of course but I don't answer duplicates. Anyway, here's code detailing the two variants: https://jsfiddle.net/khrismuc/32v5m97r/ –  Jan 15 '21 at 13:17

1 Answers1

1

You're mixing async/await and using promise.then -- you can just write it all with async/await that should make it easier:

const getAllUserStats = async (login_name) => {
    const userStats = await userstats.findOne({login_name})
    if(userStats===null){
        let user = new userstats({
            login_name:login_name,
            followers: [],
            subscribers: [],
            hosts: [],
            viewers: []
        })
        await user.save();
        return user;
    }
    else{
        return userStats;
    }
}
TKoL
  • 13,158
  • 3
  • 39
  • 73
  • Although not always good practice, combining await and then is usually fine (for instance awaiting a fetch call with a chained `.json()`). However this question is about a single issue: OP simply forgot to return something from their function. It's primarily a dupe of the most linked dupe of all times: https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call –  Jan 15 '21 at 13:20