简体   繁体   中英

NodeJS mysql promise returns undefined

I'm trying to do command for discord bot that outputs integer from MySQL table.

I tried doing this with async/await, with promises, with callbacks but the result is always same. Here I'm trying it again with promises, because in past it somehow worked. Now it won't.

Here's function that returns promise:

exports.checkAccess = (user) => {
    return new Promise((resolve,reject) => {
        sql.condb.query("SELECT `accessLevel` FROM `users` WHERE `DiscordID` = '" + user + "' LIMIT 1", function(err,rows){
            if(err) reject(err);
            else resolve(rows);
        })
    });
}

And here's code that assigns result to accessLevel variable:

let accessLevel = -1;
cmds.checkAccess(message.author.id).then(rows=>{
        accessLevel = rows[0].accessLevel;
    }).catch((err)=>{
        console.log(err);
});

Catch function catches error that says "TypeError: Cannot read property 'accessLevel' of undefined".

It seems that your query does not return any results rather then undefined promise.

I suggest you to put your query into variable and use console.log to get generated query so that you can check your final query.

let query = "SELECT `accessLevel` FROM `users` WHERE `DiscordID` = '" + user + "' LIMIT 1";
console.log(query);

Try to select stdout query into your mysql editor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM