Part of Code:
From Service part:
$resource('/Serializer/community/V1/latestPost/:start/:end,{
},{
query: {method:'GET',
params:{start:new Date().getTime(), end:1},
isArray:true}
})
From Controller part:
Post['latestPost_timestamp'].query({start: minTime,end:1},function (data) {
alert(JSON.stringify(data));
});
Result:
From console or link open on browser: [3993,3983,3974,3964,3954,3944,3934,3926,3920,3910]
From controller: [{},{},{},{},{},{},{},{},{},{}]
Why it return empty {} in controller? I would like it to be return in [3993,3983,3974,3964,3954,3944,3934,3926,3920,3910] but not [{},{},{},{},{},{},{},{},{},{}].
Can anyone help? Thanks
That's because a $resource should be an object and not a primitive, like a number in your case. If a resource could be a primitive, then you would have a problem with being able to both do this:
var posts = Post.query({start: minTime,end:1}, function() {
for (var i = 0; i < posts.length; i++) {
var post = posts[i];
// If post here should be equal to for example 3993 in
// your JSON, you wouldn't be able to call any methods
// on it. The following two statements sort if illustrates it:
assert(post === 3993);
post.$save();
}
});
A resource is an object because of this, so you should probably change your server code to return something like [{id: 3993}, {id: 3983}, ...] instead, so that you can access the id by post.id.