0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.