samedi 15 juillet 2017

How to iterate and get the properties and values of a JSONAPI response in emberJS?

I have following ember request

 this.store.createRecord('food_list',requestObj
        ).save().then((response) => {
console.log(response);
console.log(response.id); // This is working
console.log(response.food_list_code); //this does NOT work !!!!!
}

It will call an API and save a record to database and then returns following response.

{
  "links": {
    "self": "/api/food_list"
  },
  "data": {
    "type": "",
    "id": "da6b8615-3f4334-550544442",
    "attributes": {
      "food_list_date": "2013-02-14 23:35:19",
      "food_list_id": "da6b8615-3f4334-550544442",
      "food_list_code": "GORMA",
    },
    "relationships": {
      "food_list_parameters": {
        "data": [
          {
            "type": "food_list_parameter",
            "id": "RERAFFASD9ASD09ASDFA0SDFASD"
          }
        ]
      },
      "food_new_Name": {
        "data": {
          "type": "food_new_Name",
          "id": "AKASDJFALSKDFKLSDF23W32KJ2L23"
        }
      }
    },
    "links": {
      "self": "/api/BLAH/BLAH/BLAH"
    }
  }
}

but since above response is a JSONAPI in form of an ember object, I dont know how to parse it. If I try to get response.id, I get the string da6b8615-3f4334-550544442

But how to get value for food_list_code in response block. Or how to iterate the response object to get "food_list_code" and "food_list_date" ?

The output for console.log(response) is as following ember class

Class {__ember1500143184544: "ember1198", store: Class, _internalModel: InternalModel, currentState...

I appreciate your help. M.




Aucun commentaire:

Enregistrer un commentaire