John Kerim
John Kerim

Reputation: 7

How to render multiple sql query and data in Nodejs

I am having problems rendering multiple data query to page.I have done a lot of research but getting an error like Failed to look up view my code is following:

app.get('/xem', function(req,res){
pool.query("SELECT * FROM phim WHERE slider = '1' ORDER BY id DESC Limit 9", function (error, result, client){
    if (!!error){
        console.log('Error query');
    } else {
        res.render('xem', {slider:result});
    }
});
pool.query("SELECT * FROM phim WHERE new = '1'", function (error, result, client){
    if (!!error){
        console.log('Error query');
    } else {
        res.render('xem', {new:result});
    }
}); 
});

When run it code i give error:

82|     <!-- END HEAD -->

83| <h1> ok </h1>

>> 84| <%= new %>

85| 

new is not defined

How to fix it?

Upvotes: 0

Views: 2208

Answers (4)

Linux
Linux

Reputation: 343

app.use('/', (req,res) => {
    connection.query('select * from users', function(err, rows, fields){
      if(err) return;
      console.log(rows[0]);
      res.send(rows);
  });
});

Upvotes: 0

John Kerim
John Kerim

Reputation: 7

Thank you everyone. I had it working, sample code:

app.get('/xem', function(req,res){
    pool.query("SELECT * FROM phim WHERE slider = '1' ORDER BY id DESC Limit 9", function (error, result, client){
        var result1 = result;
        link('https://drive.google.com/file/d/0BxG6kVC7OXgrQ1V6bDVsVmJMZFU/view?usp=sharing', function(data){
            var dataxem = data;
            pool.query("SELECT * FROM user", function (error, result, client){
                var user = result;
                res.render('xem', {slider:result1, link:data, user:user});
            });
        });
    });
})

Upvotes: 0

strah
strah

Reputation: 6732

There are two issues with your approach:

  1. res.render() ends the http request, therefore it will fail when called more than once.
  2. You are executing two asynchronous functions and you don't take care of the order of execution

Try this:

var async = require('async');
app.get('/xem', function(req,res){
    var final = {};
    async.series({
        slider: function(cb) {
            pool.query("SELECT * FROM phim WHERE slider = '1' ORDER BY id DESC Limit 9", function (error, result, client){
                cb(error, result);
            })
        },
        new: function(cb){
            pool.query("SELECT * FROM phim WHERE new = '1'", function (error, result, client){
                cb(error, result)
            })
        }
    }, function(error, results) {
        if (!error) {
            res.render('xem', results);
        }
    });
});

I don't know if your pool uses promises, so just in case this code uses async approach

Upvotes: 1

abdulbari
abdulbari

Reputation: 6242

The code which you have written is not correct for both queries.

You will get always first query result in response and in first query result you are sending slider as key and expecting name in response

res.render('xem', {slider:result});

change it with

res.render('xem', {new:result});

Since you are giving name key is in second query result which is not reachable in your case

Upvotes: 0

Related Questions