0

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?

4 Answers 4

1

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

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

Comments

0

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

Comments

0

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});
            });
        });
    });
})

Comments

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

1 Comment

Probably an explanation would be also helpful.

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.