0

I am new to Javascript and Angular. I am trying to move from an $httpbackend mock server using straight JSON data from a file like [{"firstName":"James",...}...] to getting the data from a live REST server to try out my client code (backand).

On my client I use the http service as follows:

return $http.get(serviceUrlBase).then(getAllcontactsSuccess, getAllcontactsError);
// Promises
function getAllcontactsSuccess(response)
{
  return response.data.data;
}

The 'response data.data' returns this:
{"data":[{"__metadata":{"id":"1"},"id":1,"firstName":"James","lastName":"Bud","email":"[email protected]","phone":"504-621-8927","street":"6649 N Blue Gum St","city":"New Orleans","state":"LA","zip":"70116"},{"__metadata":{"id":"2"},"id":2,"firstName":"Josephine","lastName":"Darakjy","email":"[email protected]","phone":"810-374-9840","street":"4 B Blue Ridge Blvd","city":"Brighton","state":"MI","zip":"48116"},...]}

I read a lot of similar questions and I am trying to write an httptransformer but whatever I try is not working. I cant get rid of the "__metadata":{"id":"1"}, section which seem to be the issue as it is seen as a additional field when carrier over in the controller.

Any ideas is appreciated.

2
  • just strip out the __metadata in the service call? Commented Aug 26, 2016 at 18:14
  • Why do you need to get rid of it? What problem is it causing? can't you just... not use it? Commented Aug 26, 2016 at 18:16

1 Answer 1

2

Well you should mock the data to be the same as the actual service call, but in the service call, you can do something like:

data = response.data.data
data.forEach(function (d) {
  delete d.__metadata
})
Sign up to request clarification or add additional context in comments.

3 Comments

good question, I mean I've said some pretty dumb shit on S.O., but I feel like this was fairly reasonable :)
This does work. Feels too simple. I was lost in http transformers. I cannot understand why the metadata are not screened out of the angular response. i need to do this becasue my view code <th ng-repeat="(key,value) in ctrl.contacts[0]">{{key}}</th> was messed up in taking the __metadata as a key. Coming from C++ the delete keyword is something you have to be very careful using
transformers are for global transformations, i.e. for every single request that comes through. If all of them have __metadata you could use the transformations.

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.