Search code examples
javascriptasync-awaitarrow-functions

"await is only valid in async functions" while it's an async function


I'm getting this error for some reason:

SyntaxError: await is only valid in async functions and the top level bodies of modules

I've no idea what's wrong with it. It's obviously an async function.

const muteCheckTimer = async (client) => {
setInterval(() => { 
    db.each(query, [], (err, row) => {
        if(err) {
            console.log(err);
            return;
        }
        if(row) {
            let currentTimestamp = Date.now();
            if(currentTimestamp > row.mutedTimestamp) {
                let user = await client.users.fetch(row.userID);
                if(!user.member) { console.log('Not a user') }

                let MutedRole = user.member.guild.roles.cache.find((r) => r.name === "Muted");
                if(!MutedRole) { console.log("Muted role doesn't exists") }
                
                //await user.member.roles.remove(MutedRole);
                
            } else {
                console.log('MUTED');
            }

        }

    });
}, 10000);

}


Solution

  • Your inner function is not async.