0

Hi

I have this query on node.js. This is my server side file:

    function doquery(select,from,where,value) {
     var con = mysql.createConnection({
          host: "localhost",
          database: "db545",
          user: "root",
          password: "mypass"
        });
        
     return con.query("SELECT " + select + " FROM " + from + " WHERE " + where + value);
    };
    
   io.on('connection', (socket) => {
      // when the client emits 'add user', this listens and executes
      socket.on('add user', (username) => {
        dispname = doquery('name','members','member_id','1');

but the variable dispname is resulting in [object object] but since this is server side Im not getting any errors so I cant debug it. What is wrong?

Thank you.

6
  • seems that everything is fine, it returns the result object (handler) Commented Sep 18, 2021 at 17:10
  • "so I cant debug it" << you could attach a debugger to your process. You use a locl debugger to debug a remote process by forwarding port 9229 from your server via SSH, sending SIGUSR1 to the process to enable debug mode (or enable it from the start by passing --inspect to node) and then connecting with the Chrome devtools' "node" button which will appear in the toolbar at that point - or VSCode's debugger. In VSCode you can even use the remote mode (bottom-left corner) to do the whole thing with a nice interface. This will allow you to debug code running on a server. Commented Sep 18, 2021 at 17:13
  • how can I do that? Commented Sep 18, 2021 at 17:14
  • Also I think the issue is that you are getting a promise returned and you don't await it. (It's not clear because you didn't show which mysql package you are using exactly, there are multiple.) Commented Sep 18, 2021 at 17:15
  • 1
    but do what? con.query doesn't return the result... it has a callback to return the result, you are just doing it wrong, read the documentation... con.query(your_query, function (error, results, fields) {...} Commented Sep 18, 2021 at 17:16

1 Answer 1

2

[object Object] is the default string representation of an object. If you want to properly debug the result, you could serialize it to JSON:

console.log(JSON.stringify(dispname));

Side note:
By concatenating strings to your SQL query like that you're making the application vulnerable to SQL Injection attacks. I strongly recommend you look into prepared statements instead.

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

3 Comments

I cant log it into the console because its server side. Also, I tried JSON.stringify(dispname) but my app crashes. I get a 500 error.
Why can't you look at your server's console then?
I did but Im getting nothing.

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.