2

I am loading data from a json file in a html table,

This is the method in my component file:

getParticulars(){
        this._particularService.getParticulars().subscribe(
            result => {
                console.log("code: ", result.code);
                if(result.code != 200){
                    console.log("Respuesta: ", result);
                }else{
                    console.log("Si tengo datos para particulares");
                    this.particulars = result.data;
                }

            },
            error => {
                console.log(<any>error);
            }
        );
    }

This is the method in my service file:

getParticulars(){
    return this._http.get(this.url).map(res => res.json());
}

This is the file json:

[
    {
      "code": "200"
    },
    {
      "field1": "1",
      "field2": "2",
      "field3": "3",
      "field4": "31/12/9999",
      "field5": "4,
      "field6": "5",
      "field7": "7",
      "field8": "8",
      "field9": "9",
      "field10": "10"
    },
    {
      "field1": "11",
      "field2": "12",
      "field3": "11/07/2017",
      "field4": "13",
      "field5": "14",
      "field6": "15",
      "field7": "16",
      "field8": "17",
      "field9": "18",
      "field10": "19"
    }
  ]

The problem is, the response service code (result.code) value is undefined, but I don't know Why?

console.log("code: ", result.code);

If the result code is undefined, I am going to access in the condicion, when the result code is different to 200, but it shows the json file correct in console browser.

console.log("Respuesta: ", result);

Thanks,

2 Answers 2

3

result is an array, so you should specifiy the index,

console.log("code: ", result[0].code);
Sign up to request clarification or add additional context in comments.

Comments

0

result[0].code will do the trick, which is zeroth index of an result array

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.