0

I have a weird problem.

First I retrieve several calls at the same time. And save the returned data in a variable called "values"

  function PrefsService($resource,PrefsResource,$q) {

    var initialize = function() {

      return $q
        .all(
          [PrefsResource.get({key:"TwentyFourHourTime"}),
          PrefsResource.get({key:"DecimalTime"}),
          PrefsResource.get({key:"startDayOfWeek"}),
          PrefsResource.get({key:"RoundingIncrement"}),
          PrefsResource.get({key:"RoundingOption"})
        ]
        )
        .then(function(values) {

          return values
        })

I use this piece of code in controller to see the returned value:

PrefsService
.initialize()
.then(function(values) {
console.log("values",values);
console.log("values[0]",values[0]);
console.log("values[0].result",values[0].result);
})

I want to use "values[0].result" get the result object. But it always gives me a value of "undefined".

enter image description here

Why?

Thx

Bluesea
  • 135
  • 1
  • 1
  • 9
  • 2
    values[0].result, try this – RBoschini Dec 09 '15 at 16:31
  • 1
    At what point are you attempting to use `values[0].result`? [This may be related](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call). – James Thorpe Dec 09 '15 at 16:34
  • What you are explaining should work. I'm guessing there's more to it than your explanation. – Kevin B Dec 09 '15 at 16:36
  • The return from initialize will be a promise. Are you calling initialize.then(...)? – Amy Blankenship Dec 09 '15 at 16:39
  • With what you have shown, values[0].result is the correct way of getting it..May be you are accessing it before it resolves – Saurabh Tiwari Dec 09 '15 at 16:39
  • Hi all, I have updated the question detail.... – Bluesea Dec 09 '15 at 16:43
  • You've updated it, but you appear to be logging `values.values` and `values.values.result`? And have strange syntax as pointed out by Jamiec - please ensure this is a true representation of your actual code. – James Thorpe Dec 09 '15 at 16:47
  • Yes. I want to get the "result" object. – Bluesea Dec 09 '15 at 16:49
  • @Bluesea - then you should do the work on the promises inside the `then` callback in the `PrefsService` so that you return the actual values to whatever calls `PrefsService.initialize()` instead of returning the promises, which is what it's doing right now. – Adam Jenkins Dec 09 '15 at 17:04
  • I have updated the question again. Thx all. – Bluesea Dec 09 '15 at 17:15

3 Answers3

0

This syntax looks weird:

return {
    values
}

Its basically an object literal with a property name, but no value. In any case what you are tagging on to the initial all is unnecessary:

.then(function(values) {

      return {
        values
      }
    })

Just remove that part.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

The returned values are promises, use them as promises like you are supposed to:

PrefsService
.initialize()
.then(function(values) {
    values.map(function(valuePromise) {
       valuePromise.then(function(value) {
           console.log(value);
       });
    });
 });
R. Salisbury
  • 1,954
  • 16
  • 17
  • Because you're wrapping `values` in an object. Just return `values` not `{values}` and you will get an array, which will have the `map()` function. – R. Salisbury Dec 09 '15 at 16:57
  • 1
    That's because your doing this `return { values };` What you should be doing is `return values;` – Adam Jenkins Dec 09 '15 at 16:57
0

The most straightforward way would be to return the actual values, not the promises:

  function PrefsService($resource,PrefsResource,$q) {

    var initialize = function() {

      return $q
        .all([
          PrefsResource.get({key:"TwentyFourHourTime"}),
          PrefsResource.get({key:"DecimalTime"}),
          PrefsResource.get({key:"startDayOfWeek"}),
          PrefsResource.get({key:"RoundingIncrement"}),
          PrefsResource.get({key:"RoundingOption"})
        ])
        .then(function(values) {
          var returnValues = [];
          values.forEach(function(v) { 
              v.then(function(a) { 
                  returnValues.push(a); 
              })
          });
          return returnValues;
        })
    };

    return {
        initialize:initalize;
    }
}


PrefsService
   .initialize()
   .then(function(values) {
      console.log(values); //an array of the actual values, not the promises
   })
Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100
  • TypeError: v.then is not a function at http://localhost/services/prefsService.js:27:13 at Array.forEach (native) – Bluesea Dec 09 '15 at 17:09