0
votes

I have a Rails backend that would serve JSONs like this: (This e.g. is 2.json)

{"id":2,"name":"Magic","location":"Cyberjaya","surprise_type":"Great","instructions":"test","status":"awesome","pricing_level":3,"longitude":"2.90873","latitude":"101.655027","created_at":"2016-02-02T07:19:18.247Z","updated_at":"2016-02-02T12:59:14.403Z"}

and an AngularJS app.js code that goes like this (the relevant parts only):

.factory('MarkersLoc', function($resource) {
  debugger;
  return $resource("http://localhost:3000/surprises/:id.json");
})

.factory('Markers', function(MarkersLoc) {

  var markers = [];

  return {
    getMarkers: function(){
        return MarkersLoc.query().$promise.then(function(response){
          debugger;
          markers = response;
        console.log("Markers: ", markers);
          return markers;
      });

    }
  }
})

The problem that I'm having is that, even at the point of the debugger, response is an array of objects that have all the attributes from the JSON except longitude, latitude, created_at and updated_at.

Unfortunately I am unable to determine the reason why these four key-value pairs are omitted when it's being read and returned. Is there something that should be controlling which key-value pair is being processed that I have left out on? Wouldn't the entire JSON be parsed and converted into the object?

1
What is the JSON sent to the browser? It is unlikely that data will "disappear" when JSON-decoded if it is indeed part of the response. - user2864740
Do you use a serializer in your Rails application? Look for any file with a name similar to *_serializer.rb. - gnerkus
@user2864740 - Loading the json URL directly returned the above. Would it serve it differently to a browser as compared to what I would see if I went on the link to the json directly? (This being localhost:3000/surprises/2.json) - Joel Lim
@gnerkus - I just looked and found on the Rails side app/serializers/surprise_serializer.rb, with this code: class SurpriseSerializer < ActiveModel::Serializer attributes :id, :name, :location, :surprise_type, :instructions, :status, :pricing_level end I will try changing this file and get back on if it is the cause of it - Joel Lim
@JoelLim Use the Network Log/Monitor (in the Browser Developer Tools) to view what the client actually gets. Nothing else is immediately relevant until actual returned response has been verified. - user2864740

1 Answers

0
votes

The missing values are the result of the data being serialized by the controller on the backend. To view all the keys, ensure all the attributes are defined in the serializer:

class SurpriseSerializer < ActiveModel::Serializer
  attributes :id,
             :name,
             :location,
             :surprise_type,
             :instructions,
             :status,
             :pricing_level,
             # Add the missing attributes to the serializer
             :longitude,
             :latitude,
             :created_at,
             :updated_at
end