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?

6
  • Have you tried returning something from the getAllUserStats function? eg return await userstats... Commented Jan 15, 2021 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. Commented Jan 15, 2021 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? Commented Jan 15, 2021 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 Commented Jan 15, 2021 at 13:12
  • @TKoL This is exactly what I did now. Thanks a lot! Commented Jan 15, 2021 at 13:15

1 Answer 1

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;
    }
}

Sign up to request clarification or add additional context in comments.

1 Comment

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: stackoverflow.com/questions/14220321/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.