0

When define response data to array, error occured:

public esriLocation = [];
public testLocation  = [];

 this.service.getData().subscribe(
                  (response) => this.esriLocation = response.json(),
                  (error) => console.log('ESRI ERROR: ' + error)
          );

          this.testLocation = this.esriLocation.suggestions;

Property 'suggestions' does not exisits on type 'any'

Responce

{
    "suggestions": [{
        "text": "DEU",
        "key": "dHA9MCNsb2M9MTAzOTkwNTEjbG5nPTQ0I2xicz0xMDk6NDI2MDI4NzM=",
        "isCollection": false
    }, {
        "text": "ENG",
        "key": "dHA9MCNsb2M9MTA0MDAzOTcjbG5nPTQ0I2xicz0xMDk6NDI2MDI4NzU=",
        "isCollection": false
    }]
}

How to fix?

4
  • try declare public esriLocation = {}; Also what is the value of console.log(this.esriLocation after the service call Commented Dec 7, 2017 at 7:55
  • 2
    try to declare like : public esriLocation:any; public testLocation:any; Commented Dec 7, 2017 at 8:00
  • @AddWebSolutionPvtLtd Thanks it Helps! Commented Dec 7, 2017 at 8:02
  • if it work then please upvote Commented Dec 7, 2017 at 8:06

2 Answers 2

3

try to declare like below :

public esriLocation:any; 
public testLocation:any; 
    this.service.getData().subscribe(
                      (response) => {this.esriLocation = response.json()
                                     this.testLocation = this.esriLocation.suggestions;
                               },
                      (error) => console.log('ESRI ERROR: ' + error)
              );
Sign up to request clarification or add additional context in comments.

Comments

2

It is async by nature you need to do the assignment inside the success call back.

public esriLocation = [];
public testLocation  = [];

 this.service.getData().subscribe(
                  (response) => {this.esriLocation = response.json()
                                 this.testLocation = this.esriLocation.suggestions;
                           },
                  (error) => console.log('ESRI ERROR: ' + error)
          );

as it is async by nature you cannot guarantee that the result will be available after the subscribe block, if it is available and you find it not assigned properly check if you are getting a JSON object not the string.

2 Comments

check if you have the typeof this.esriLocation is actually an object
@AddWebSolutionPvtLtd provided a fix. Thank you for help, your answer are helpfull too

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.