2

I might just be blind or something, but I really cannot figure out why I cannot access a sub object of a returned $Resource object that retrieved a bunch of JSON objects.

Resource
> $resolved: true
> $then: function (b, g) {var j=e(),h=
> data: Object
  > 519bc5f6b2427a732be1c360: Object

The raw JSON looks like this:

{
    "data": {
        "519bc5f6b2427a732be1c360": {
            "id": "519bc5f6b2427a732be1c360",
            "planning": {
                "id": "519bc5f6b2427a732be1c355"
            }
        }
    }
}

Can anyone explain me why this doesn't work:

var training = Training.query()

console.log(training); // returns the entire $Resource
console.log(training.data); // returns: undefined 
9
  • A scientific guess: felix-kling.de/blog/2011/08/18/… PS: If I were you I would put a break (just a regular one or using debugger;) and check with debugger what is the actual value (that's right, you can trust a debugger, and you cannot trust console.log()) Commented May 25, 2013 at 12:23
  • .query() returns an array Commented May 25, 2013 at 12:24
  • @user1737909 Good point, but that's already solved in my services layer, didn't add that; I have manually set .query() to return isArray: false; Commented May 25, 2013 at 12:26
  • 1
    Because you are missing a closing }? Commented May 25, 2013 at 12:28
  • omg, instead of checking and getting the data for sure you guys prefer continue guessing.......... It's so weird way to program, really Commented May 25, 2013 at 12:30

2 Answers 2

2

Here is the explanation - from the Angular docs:

It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray). Once the data is returned from the server the existing reference is populated with the actual data. This is a useful trick since usually the resource is assigned to a model which is then rendered by the view. Having an empty object results in no rendering, once the data arrives from the server then the object is populated with the data and the view automatically re-renders itself showing the new data. This means that in most case one never has to write a callback function for the action methods.

So this works:

var training = Training.query(function(value){
  // this is the callback function
  console.log(training === value); // true - it's the same object
  console.log(training.data); // and now it has data
});
Sign up to request clarification or add additional context in comments.

Comments

1

Try getting the data like this:

var training = Training.query(function($val) {
    console.log($val);
    console.log($val.data);
});

1 Comment

This works! I've got another problem now, but I might be able to figure this out. Thank you for your contribution! The closure function seemed to work for some reason, but I still deem this weird and unexpected behavior.

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.