2016-09-30 30 views
0

對AngularJS和Node.js不熟悉。請指教。使用AngularJS和Node.js進行異步調用

因爲我想在頁面上顯示的數據需要一段時間才能加載。我決定首先從database_1加載快速數據,然後再從數據庫2中獲得緩慢的響應。這裏是我的AngularJS:

var app = angular.module('myApp', [ 

]); 

app.factory('rateFactory', function ($http) { 
    return $http.get("localhost/rate"); // api or node.js to return only issueId and rate 
}) 

app.controller('SignupController', function ($scope, $http, $filter, rateFactory) { 
    // Display most of the content first 
    $scope.showData = function() { 
     $http.get("localhost/signup") // api or node.js 
     .success(function (response) { 
      $scope.Signups = response; 

      $scope.curPage = 0; 
      $scope.pageSize = 25; 
      $scope.numberOfPages = function() { 
       return Math.ceil($scope.Signups.length/$scope.pageSize); 
      }; 
     }) 
     .error(function (data, status, headers, config) { 
      alert(status); 
     }); 
    } 

    // Display slow response, Rate, later based on the issueId 
    $scope.showRate = function (issueId) { 
     rateFactory 
     .success(function (data) { 
      document.getElementById(issueId).innerHTML = data.find(x => x.IssueID === issueId).Rate; 
     }) 
     .error(function (data, status, headers, config) { 
      //alert(status); 
     }); 
    } 
}); 

我不知道是否有更好的方法來做到這一點。這是我的第一個問題。 下一個問題是關於Node.js.如果我從ashx或api獲取數據,它將毫無問題地返回數據。但是,當使用Node.js進行這兩個調用時,這是一個命中和錯過。有時候它可以正常工作,但大多數情況下,第二次調用失敗。難道我做錯了什麼?如果單獨調用,兩者都會完美返回數據。這裏是Node.js的代碼:

var express = require('express'); 
var http = require('http'); 
var app = express(); 
var usp2Json = require('./lib/usp2Json.js'); 

app.get('/iisnode/myApp/signup', function(req, res) { 
    usp2Json.getJsonFromStoredProc('stroedprocToGetSignup', req, res); 
}); 

app.get('/iisnode/myApp/rate', function(req, res) { 
    usp2Json.getJsonFromStoredProc('stroedprocToGetRate', req, res); 
}) 

var server = http.createServer(app); 
var port = process.env.PORT || 593; 
server = app.listen(port, function() { 
    console.log('Server is running...'); 
}); 

usp2Json.js是一個自定義模塊,具有存儲過程從SQL Server數據:

exports.getJsonFromStoredProc = function(storedproc, req, res) { 
var sql = require("mssql"); 
var express = require('express'); 
var app = express(); 

res.header('Access-Control-Allow-Origin', '*'); 
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); 
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With'); 

// config database 
var config = { 
    user: 'username', 
    password: 'password', 
    server: 'servername', 
    database: 'databasename', 
}; 

// connect to database 
sql.connect(config, function(err) { 
    if(err) console.log(err); 

    // create Request object 
    var request = new sql.Request(); 

    // query to the database and get the records 
    request.query(storedproc, function(err, recordset) { 
     if(err) 
      console.log(err); 

     // send records as a response 
     res.send(recordset); 
    }); 
}); 
} 
+0

什麼是'usp2Json.getJsonFromStoredProc'的代碼? – nikjohn

回答

0

就NodeJS API調用而言,問題實際上是SQL Server連接。當我查看console.log時,它沒有給我足夠的信息,但只是說「服務器正在運行」。我不得不添加一行得到錯誤的詳細信息:

request.query(storedproc, function (err, recordset) { 
     if (err) {     
      fs.appendFile("path"+ datetime+".txt", time + " Error on executing " + storedproc + " - " + err + " \r\n") 
      //throw (err); 
     } 

     // send records as a response 
     res.send(recordset); 
    }); 

這給了我「ConnectionError:連接被關閉。」有了這些信息,我能找到從這裏的解決方案:

https://github.com/patriksimek/node-mssql/issues/138

和#1回答:How can I use a single mssql connection pool across several routes in an Express 4 web application?

0

一個建議,我已經是擺脫:

document.getElementById(issueId).innerHTML = data.find(x => x.IssueID === issueId).Rate; 

和使用正角2個數據綁定,和你的#issueId元素綁定到$scope.issueId範圍變量,更新呼叫成功的scope變量。它現在完成的方式有點像反模式。

就NodeJS API調用而言,您需要告訴我們路由處理程序(即usp2Json.getJsonFromStoredProc)在其代碼中所做的事情。否則你的代碼看起來非常好

+0

感謝您的及時回覆。問題用usp2Json代碼更新。 – Zerubbabel

+0

不確定如何使用2種方式的數據綁定,因爲使用ng-repeat顯示的結果是,而不是input/textarea。工廠只返回issueId和費率。 – Zerubbabel