0

I am new to node.js, I want to input output of a query to another query please help

 pool.getConnection(function(err, connection) {
   connection.query("select * from tbl_chat", function(err, rows) {
       console.log(rows[0]['from_id']);
       var fromid = rows[0]['from_id'];
   });
   console.log(fromid);//throws ReferenceError: fromid is not defined
   console.log(rows[0]['from_id']);// throws ReferenceError: rows is not defined

  //I want to use the fromid in the following query 

   /*connection.query("select * from tbl_chat where from_id=?",[fromid], function(err, rows) {
       console.log(rows[0]['from_id']);
   });*/
 });

1 Answer 1

1

NodeJs database query are asynchronous, so you have to put your console.log in the callback, or do it with promises.

Try it:

 pool.getConnection(function(err, connection) {
   connection.query("select * from tbl_chat", function(err, rows) {
       console.log(rows[0]['from_id']);
       var fromid = rows[0]['from_id'];
       console.log(fromid);
       console.log(rows[0]['from_id']);
       connection.query("select * from tbl_chat where from_id=?",[fromid], function(err, rows) {
           console.log(rows[0]['from_id']);
       });
   });
 });
Sign up to request clarification or add additional context in comments.

Comments

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.