我有一个Rails后端,可以像这样服务JSON :(例如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"}
和一个类似的AngularJS app.js代码(仅限相关部分):
.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;
});
}
}
})
我遇到的问题是,即使在调试器的时候,响应也是一个对象数组,它包含JSON的所有属性,除了经度,纬度,created_at和updated_at。
不幸的是,在读取和返回这四个键值对时,我无法确定这四个键值对的原因。有什么东西应该控制我正在处理哪个键值对?难道不会解析整个JSON并将其转换为对象吗?
答案 0 :(得分:0)
缺失值是由后端控制器序列化数据的结果。要查看所有键,请确保在序列化程序中定义了所有属性:
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