0

I am new in nodejs.I am developing a demo app using nodeJs.I have problem while run multiple queries.My Code is like:

exports.dashboard = function(req, res){

 req.getConnection(function(err,connection){

 var id = req.session.userId;
  var queryData =  "SELECT node_questions.question_name,node_questions.description FROM node_questions LEFT JOIN node_user ON node_questions.user_id=node_user.id";
        var query = connection.query(queryData,'SELECT * FROM node_user Where id = ?',[id],function(err,rows)
        {
                console.log(rows);
               res.render('dashboard',{page_title:"Dashboard",session:req.session.userId,email:req.session.email,data:rows});
         });
    });  
};

Database connection is:

 app.use(
        connection(mysql,{
            host: 'localhost',
            user: 'root',
            password : 'password',
            port : 3306, //port mysql
            database:'nodeDemo',
            multipleStatements:true

        },'pool') //or single

    );
5
  • Please could you provide more information on the error? Commented Oct 23, 2015 at 8:12
  • This is error: "TypeError: undefined is not a function" Commented Oct 23, 2015 at 8:15
  • Does that error give you a stack trace? Commented Oct 23, 2015 at 8:17
  • Can You Please tell me how can run multiple mysql queries in nodeJs? Commented Oct 23, 2015 at 8:19
  • Undefined not a function error does not mean the issue is to do with running multiple queries but without a stack trace it's difficult to help. Can you post the error stack trace to your question? Commented Oct 23, 2015 at 8:55

2 Answers 2

1

You are mixing things together. For example what would you expect your rows to be ? Should it be the result-set of the first or the second query ? A good way to handle this would be to use async module (ensure you install it via npm in your package.json first).

var async = require('async');
var mysql      = require('mysql');

client = mysql.createConnection({
    user: 'root',
    password : 'password',
    port : 3306, //port mysql
    database:'nodeDemo',
    multipleStatements:true
});

var id = req.session.userId;

async.parallel([
    function(callback) {
        var queryData = '' +
            ' SELECT node_questions.question_name,node_questions.description' +
            ' FROM node_questions' +
            ' LEFT JOIN node_user' +
            ' ON node_questions.user_id=node_user.id';
        client.query(queryData, function (err, rows1) {
            if (err) {
                return callback(err);
            }
            return callback(null, rows1);
        });
    },
    function(callback) {
        client.query('SELECT * FROM node_user Where id = ?', id, function (err, rows2) {
            if (err) {
                return callback(err);
            }
            return callback(null, rows2);
        });
    }
], function(error, callbackResults) {
    if (error) {
        //handle error
        console.log(error);
    } else {
        console.log(callbackResults[0]); // rows1
        console.log(callbackResults[1]); // rows2
        // use this data to send back to client etc.
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Kaya Toast: I got error "ReferenceError: client is not defined"
Basically, my answer was merely illustrative on how to treat two separate queries and get responses. Anyway, I've defined client now.
Glad to be of help. At some point, explore further details on why app.use, why pool, async.series etc. Good luck with your project :-)
1

why not use Promise?

let Promise = require('bluebird'),
    begin = []

begin.push(promise1(a,b))
begin.push(promise2(c.d))

return Promise.all(begin)
    .spread(function (resultFromQuery1, resultFromQuery2) {
        // do something with the result here
    })

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.