0

I am using MySQL database and have created table that stores user's characters names from my Discord Bot.

I want my bot to display this list on Discord after using command, but it just returns [object Object].

I tried JSON.stringify() but it doesn't work. Is there something more to it I don't know about?

con.query (`SELECT Name FROM ${message.author.username}list`, (err, rows) => {
         if(err) throw err;

         JSON.stringify(rows);
         message.reply(rows);

Both with and without stringify result are as in the picture]

1
  • As a sidenote, your code is vulnerable to a SQL injection attack. Commented Sep 29, 2019 at 11:47

1 Answer 1

1

you should save rows data in some variable. after update you code:

con.query (`SELECT Name FROM ${message.author.username}list`, 
            (err, rows, fields) => {
                  if(err) throw err;
                  rows = JSON.stringify(rows);
                  message.reply(rows);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I've tought I tried this already, but apparently not and it works like a charm, thanks! If I may ask one more question: It works perfectly and displays: {"Name":"X"} Is there a way to display only value of that row, so for instance to display only "X" without "Name:"?
Thanks, Yes there is. You need to process it before stringing that object (by looping or modules used to process objects) then put all the values into an array, for example, and send the array .

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.