4

i have this function

searching() {
 this._demoService.postData(this.url,this.searchtobesent).subscribe(
  data =>{
    this.listings=data;
    this.errorMsg="";
    if(data){
    }
    else{
      this.errorMsg="Nothing Found";
    }
  },
  error =>{
    console.log("error : "+error);
    this.errorMsg="Error Loading Your Listings";
  }
  ) ;
}

When there are some results from my backend (that are being sent in Json format) everything is ok. what i want is when the results are empty and i receive from the server this

[]

i want this.errorMsg to become Nothing Found. Any help would be appreciated ;) Thanks

1
  • Is your application making service calls to a different port? If so which framework are you using (.NET, Spring, etc) Commented Apr 23, 2017 at 0:16

1 Answer 1

5

Even it is an empty array, it is still a valid response. You can use array.length to handle this.

if (data && data.length > 0) {
    // do something
} else {
    this.errorMsg = "Nothing Found";
}
Sign up to request clarification or add additional context in comments.

Comments

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.