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

이 내 길이다

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

내 loanService입니다 - 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); 
     } 
    }) 
} 

});

답변

3

변경

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

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

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

확인 .... 그래서 난 템플릿에 이제 데이터를 사용할 수 있습니다 (collections.html) 직접 :

는 모습이 위대한 게시물을 가져? –

+0

@hemanthkoganti는 사용 방법에 따라 다릅니다. 귀하의 질문에 HTML을 포함하십시오. – echonax

+1

네, 그럴 수있어 ........ thanks @echonax –

관련 문제