Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
100 views
in Technique[技术] by (71.8m points)

javascript - Discord.js mysql query error not valid input

I wanted to make a MySQL query with my Discord.js bot, however, I always get an error.

My Code:

client.on("message", async (message) => {
    if (message.content.startsWith("?userinfo")) {
        database.query("SELECT members FROM usertable WHERE username=?", [message.content], function (err, results) {
            if (err) {
                throw err;
            }
            //console.log(results[0]);

            var resultArray = Object.values(JSON.parse(JSON.stringify(results[0])));
            console.log(resultArray);
            steamid = resultArray;
        });
    }
});
question from:https://stackoverflow.com/questions/65872163/discord-js-mysql-query-error-not-valid-input

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you take over the complete message content and therefore the error occurs create a variable and give it the message content

var messageContent = message.content.slice(10);

(slice takes the complete string and truncates it to the length specified)

And then replace the message.content in the sql query with "messageContent".

example:

    client.on('message', async message => {
        if (message.content.startsWith('?userinfo')) {
            var messageContent = message.content.slice(10);
            database.query('SELECT members FROM usertable WHERE username=?', [messageContent], function (err,results) {
                if (err) {
                    throw err;
                }
                //console.log(results[0]);
    
                var resultArray = Object.values(JSON.parse(JSON.stringify(results[0])));
                console.log(resultArray);
                steamid = resultArray;
            })
    
      }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...