2017-02-06 7 views
1

サードパーティのAPIに接続するアプリケーションを記述しています。

apiは認証トークンシステムを使用しているので、まず非同期ノードのjs関数を使用してトークンを要求し、そのトークンを使用してデータを取得します。

問題は、データが変更されると、ノードjs api呼び出しでエラーがスローされても、同じデータがページに表示されるように、範囲が変更されないことです。

少しずつコードを実行します。

get-salesLead-data.js http PUTを使用してサードパーティの残りのAPIを最初に呼び出し、認証トークンを返す非同期ウォーターフォール機能を備えています。このトークンは、非同期式の水の第2の関数に渡され、その後、http://www.gemetp.jp/http://www.welcome.hp.com/hpinfo/ja/index.htmlを参照してセールスリードデータを取得します

ここでは、ノード非同期APIコールがあります。以下は

**get-saleLead-data.js** 

    // app/get-SalesLead-data.js 
    // modules ================================================= 

    var http = require('http'); 
    var express = require('express') 
    var async = require('async'); 

    module.exports = function(app) { 


     Using async to preform async api calls and passing data between them 

     async.waterfall([ 

      // First function is requesting the auth token 

      requestToken = function(callback) { 

       var options = { 
        "host": "********", 
        "path": '************' 
        "method": "PUT", 
        "headers": { 
         "Content-Type": "application/json", 
        } 
       }; 

       var login = JSON.stringify({ 

        "username": "********", 

        "password": "********", 

        "client_ip_address": "********" 

       }); 

       var req = http.request(options, function(res) { 
        res.on('data', function(body) { 
         var body = JSON.parse(body); 
         var token = body.token; 
         console.log(token); 
         callback(null, token); 
        }); 
       }); 

       req.on('error', function(e) { 
        console.log('problem with request: ' + e.message); 
       }); 

       req.write(login); 
       req.end(); 
      }, 

      // Second function is using the auth token receieved from the first function to get sales lead dat 
      getData = function(arg1, callback) { 

       // Geting the sales id from the url and using it in the api request. 
       app.get('/', function(req, res) { 

        app.set('salesLeadId', req.query.id); 
        var token = arg1; 
        var auth = 'Basic ' + new Buffer('********' + ':' + token).toString('base64'); 
        var path = '****************' + app.get('salesLeadId'); 
        var options = { 
         "host": "********", 
         "path": path, 
         "method": "GET", 
         "headers": { 
          "Content-Type": "application/json", 
          "Authorization": auth 
         } 
        }; 

        var data = ''; 

        // Assinging response data to to data 
        var req = http.request(options, function(res) { 
         res.on('data', function(chunk) { 
          data += chunk; 
         }); 

         // Creating sales lead api so the front end can make requests 
         res.on('end', function(res) { 
          var body = JSON.parse(data); 
          console.log(body); 
          app.get('/api/salesLead', function(req, res) { 
           return res.json(body); 
           $scope.$apply(); 
          }); 

         }) 

        }); 

        req.on('error', function() { 
         alert('error'); 
        }); 


        res.sendFile('index.html', { 
         root: '../vpbx/public/views' 
        }); 

        req.end(); 

       }); 

      } 

     ], function(err, result) { 

     }); 
    }; 

/API/salesLead がバックエンドusinga HTTP GETリクエストを呼び出す関数を作成するアクセスサービスです。これは販売リードデータを返します。

**salesLeadService.js** 

    angular.module('SalesLeadService', []).service('SalesLeadService', function($http, $location, $rootScope) { 

     var urlBase = '/api/salesLead' 

     this.getSalesLead = function (data) { 
      return $http.get(urlBase, data) 
     }; 
    }); 

以下がオファーコントローラです。これにより、上記のサービスが呼び出され、$scope.salesLeadにデータが割り当てられます。私は$scope.$applyを使用してみましたがhavntは、任意の運を持っていた

**offer.js** 

    // Form controller 
    // ============================================================================= 
    angular.module('offerController', []).controller('offerController', function($scope, $http, SalesLeadService, $timeout) { 

     // GETTING THE DATE-TIME STAMP 
     $scope.getDate = new Date(); 
     $scope.date = Date(); 

     // CALLING THE FUNCTION FROM THE SALES LEAD SERVICE 

     SalesLeadService.getSalesLead() 
      .then(function(response) { 
        $scope.salesLead = response.data; 
        $scope.$applyAsync(); 
      }); 
    }); 

だけのノート。 すべてのヘルプは、この方法のような物で試してみてください おかげ

答えて

0

私は、問題は、フロントエンドが、私のバックエンドをどうする波平ました。

問題は、ネストされたルートがあることでした。

問題を修正するために、私はルートを完全に書き換えました。

3

を高く評価している

$scope.object = {salesLead : ''}; 

$scope.object.salesLead = response.data; 

HTML

使用{{object.salesLead}}

私はそれがために働くだろうと思いますあなた

offer.js

angular.module('offerController', []).controller('offerController', function($scope, $http, SalesLeadService, $timeout) { 

    // GETTING THE DATE-TIME STAMP 
    $scope.getDate = new Date(); 
    $scope.date = Date(); 

    // CALLING THE FUNCTION FROM THE SALES LEAD SERVICE 

    $scope.object = {salesLead:''}; 
    SalesLeadService.getSalesLead() 
     .then(function(response) { 
       $scope.object.salesLead = response.data; 
       $scope.$applyAsync(); 
     }); 
}); 
+0

お返事ありがとうございます。私は疲れて、それはまだ更新されていません。エラーはスローされません。 –

+0

いくつかのtoruble撮影をした後、実際にノードの非同期API呼び出しだったので、それが更新されなかったことがわかりませんでした。私はlocalhost:8081/api/salesLeadをテストし、正しいJSON応答を得ました。ノードjsサードパーティのapi呼び出しが更新され、同じJSON本体をフロントエンドに送信していた後、再度実行しました。 –

関連する問題