简体   繁体   English

Angular Promise和Node.js http获得响应

[英]angular promises and nodejs http get response

I would use the promises of angularJS to fill data to a grid. 我会使用angularJS的承诺将数据填充到网格中。 I'd like to load data "row by row" as soon as the nodeJS's server, on which use the module "mssql" with the "stream" enabled, back to client every single line from the DB. 我想在nodeJS的服务器上“逐行”加载数据,在该服务器上使用启用了“ stream”的模块“ mssql”,将其从数据库返回到客户端的每一行。

On the client side I use these functions: 在客户端,我使用以下功能:

function asyncGreet() {
  var deferred = $q.defer();
  var _url = 'http://localhost:1212/test';

  $http.get(_url).
    then(function(result) {
      deferred.resolve(result);
        }, function(error) {
      deferred.reject(error);
        }, function(value) {
      deferred.notify(value); //<<-- In "value" I would like to get every single row
     });
     return deferred.promise;
  }

  $scope.btnTest = function () {
            var promise = asyncGreet();
            promise.then(function(res) {
                console.log('Success: ' + res.data + "\n");
            }, function(reason) {
                console.log('Failed: ' + reason);
            }, function(update) {
                console.log('Got notification: ' + update); //<<--
            });
        };

On nodeJS server those: 在nodeJS服务器上,这些:

app.get('/test', function (req, res) {
//sql for test
var _query = 'select top 50 * from tb_test';
var sql = require('mssql');
var connection;
var config = {
    user: 'testUser',
    password: '12345',
    server: 'localhost\\test',
    database: 'testDB',
    stream: true
};

connection = new sql.Connection(config, function (err) {
    var request = new sql.Request(connection);
    request.query(_query); 

    request.on('recordset', function(columns) {
        // Emitted once for each recordset in a query
        //res.send(columns);
    });

    request.on('row', function(row) {
        res.write(JSON.stringify(row)); //<<-- I would like intercept this event on client side 
        // and get the result in my angularJS function on deferred.notify
    });

    request.on('error', function(err) {
        // May be emitted multiple times
        console.error(err)
    });

    request.on('done', function(returnValue) {
        // Always emitted as the last one
        res.end('DONE');
    });
});

});

Anyone can help me with this? 有人可以帮我吗?

Thanks! 谢谢!

I'm done it using socket.io :) 我已经用socket.io完成了:)

On angularJS side: 在angularJS方面:

// count the row for test only
$scope.count = 0;
  $scope.prova = function () {
    mySocket.emit('getTableByRow', {});
    mySocket.on('resRow', function (data) {
      if (data.event == 'ROW') {
        $scope.count += 1;
      }else {
        $scope.count += " !!DONE!! ";
      }
   });
};

On NodeJS side: 在NodeJS方面:

[ ... connection with DB ... ]

io.on('connection', function (socket) {
  socket.on('getTableByRow', function (data) {
      _getTableByRow(socket, data);
  });
});

_getTableByRow function: _getTableByRow函数:

var _getTableByRow = function (socket, data) {
  var _query = 'select top 50 * from tb_test';
  request.query(_query);

  request.on('row', function(row) {
    // return only the ids for test 
    socket.emit('resRow', {event: 'ROW', data: row.id.toString()});
  });

  request.on('done', function(returnValue) {
    socket.emit('resRow', {event: 'DONE'});
  });

  request.on('recordset', function(columns) {
    console.log(columns);
  });

  request.on('error', function(err) {
    socket.emit('resRow', {event: 'ERROR', data: err});
  });
}

In this way, as soon as one row is read from the DB, it is immediately sent to the client :) 这样,一旦从数据库中读取了一行,它就会立即发送给客户端:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM