如何在Nodejs中呈现多个sql查询和数据

时间:2017-04-13 13:49:32

标签: node.js render

我在向页面呈现多个数据查询时遇到问题。我已经做了很多研究,但是收到了一个错误,例如找不到查看我的代码的错误:

$(document).on('click', '#disabled-radio-btn', function (e) {});

运行时代码我给出错误:

"dependencies": {
    "@angular/common": "2.4.8",
    "@angular/compiler": "2.4.8",
    "@angular/compiler-cli": "2.4.8",
    "@angular/core": "2.4.8",
    "@angular/forms": "2.4.8",
    "@angular/http": "2.4.8",
    "@angular/platform-browser": "2.4.8",
    "@angular/platform-browser-dynamic": "2.4.8",
    "@angular/platform-server": "2.4.8",
    "@ionic-native/calendar": "^3.4.4",
    "@ionic-native/contacts": "^3.4.4",
    "@ionic-native/core": "^3.4.4",
    "@ionic-native/google-plus": "^3.4.4",
    "@ionic-native/keyboard": "^3.4.4",
    "@ionic-native/splash-screen": "^3.4.4",
    "@ionic/storage": "1.1.7",
    "angularfire2": "^2.0.0-beta.8",
    "chance": "^1.0.6",
    "firebase": "^3.7.4",
    "ionic-angular": "2.3.0",
    "ionicons": "3.0.0",
    "moment": "^2.18.1",
    "ng2-translate": "^5.0.0",
    "rxjs": "^5.0.1",
    "sw-toolbox": "3.4.0",
    "uglify-js": "^2.8.22",
    "underscore": "^1.8.3",
    "zone.js": "^0.7.2"
  },
  "devDependencies": {
    "@ionic/app-scripts": "1.1.4",
    "@types/gapi": "0.0.30",
    "typescript": "2.0.9"
  },

如何解决?

4 个答案:

答案 0 :(得分:1)

您的方法存在两个问题:

  1. res.render()结束http请求,因此多次调用时会失败。
  2. 您正在执行两个异步函数,而您没有处理执行顺序
  3. 试试这个:

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

    我不知道您的pool是否使用了promises,以防万一此代码使用异步方法

答案 1 :(得分:0)

您编写的代码对于两个查询都不正确。

您将始终获得第一个查询结果作为响应  在第一个查询结果中,您发送slider作为关键字,并期待name作为回应

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

更改它
  

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

由于您提供name密钥是在第二个查询结果中,在您的情况下无法访问

答案 2 :(得分:0)

谢谢大家。我有它工作,示例代码:

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

答案 3 :(得分:0)

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