0

I am trying to get data from an API. All i get is a 204 status. I have data in my DB, which when called in POSTMAN sends proper data.

Here is the collections.ts:

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

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

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

This is my route - 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);
        }
    })
}

});

1 Answer 1

3

Change

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

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

to

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

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

  }

Since the http call is an async operation your response will take a while to process. Your this.collections will only be defined when the response arrives in the callback (subscribe).

Take a look this great post: How do I return the response from an asynchronous call?

Sign up to request clarification or add additional context in comments.

1 Comment

@hemanthkoganti depends on how you are using it. Include the html in your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.