2016-08-23 5 views
0

の入力に1つのクエリの出力を使用するには、私は別のクエリにクエリの入力出力したいのNode.js:どのように私はNode.jsのに新しいです別のクエリ

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

NodeJsを助けてくださいデータベースクエリは非同期なので、コールバックにconsole.logを入れるか、約束して実行する必要があります。

それを試してみてください。

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']); 
     }); 
    }); 
}); 
関連する問題