2017-01-18 1 views
0

私はAPIからデータを取得しようとしています。私が得るのは204ステータスです。 DBにデータがあり、POSTMANで呼び出されると適切なデータが送信されます。ここでデータを返さないAPI呼び出し。 http.get()

collections.ts次のとおりです。ここ

ngOnInit() { 
     console.log("this is collections page") 

    this.loanService.getCollectionList().subscribe(response => { 
      this.collections = response; 
     }) 
    console.log(this.collections) 
    } 

は私loanServiceは、これは私のルートである

public getCollectionList(){ 
    const path = this.basePath + '/collections'; // change the path 

    let queryParameters = new URLSearchParams(); 
    let headerParams = this.defaultHeaders; 

    // queryParameters.set('',''); 

    let requestOptions: RequestOptionsArgs = { 
     method: 'GET', 
     headers: headerParams 
     // search: queryParameters 
    }; 

    return this.http.request(path, requestOptions) 
     .map((response: Response)=> { 
      if (response.status === 204) { 
       return undefined; 
      } else { 
       return response.json(); 
      } 
     }); 
} 

です - collection.js

router.get('/collections', function (req, res, next) { 

// var errors = req.validationErrors(); 

if (errors) { 
    console.log('Errors'); 
    res.status(400); 
    res.json(errors); 
} else { 


    console.log(req.query); 

    // db.loans.find({ 'STATE' : state, 'LOAN_BRANCH' : loanBranch }, function(err, loans){ 
    db.collections.find(req.query, function (err, collections) { 
     if (err) { 
      res.status(400); 
      res.send(err); 
     } else { 
      // console.log(collections); 
      res.json(collections); 
     } 
    }) 
} 

});

ngOnInit() { 
     console.log("this is collections page") 

    this.loanService.getCollectionList().subscribe(response => { 
      this.collections = response; 
      console.log(this.collections) 
     }) 

    } 

答えて

3

変更

ngOnInit() { 
     console.log("this is collections page") 

    this.loanService.getCollectionList().subscribe(response => { 
      this.collections = response; 
     }) 
    console.log(this.collections) 
    } 

HTTP呼び出しは、あなたの応答を処理するのに時間がかかります非同期操作ですので。 this.collectionsは、応答がコールバック(購読)に到着したときにのみ定義されます。

は、この偉大なポストを見てみましょう:How do I return the response from an asynchronous call?

+0

OK ....ので、私は直接テンプレート(collections.html)で、今のデータを使用できますか? –

+0

@hemanthkogantiはあなたの使い方によって異なります。あなたの質問にhtmlを含めてください。 – echonax

+1

うん、それは気にしていない........ありがとう@echonax –

関連する問題