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

10
  • 2
    values[0].result, try this Commented Dec 9, 2015 at 16:31
  • 1
    At what point are you attempting to use values[0].result? This may be related. Commented Dec 9, 2015 at 16:34
  • What you are explaining should work. I'm guessing there's more to it than your explanation. Commented Dec 9, 2015 at 16:36
  • The return from initialize will be a promise. Are you calling initialize.then(...)? Commented Dec 9, 2015 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 Commented Dec 9, 2015 at 16:39

3 Answers 3

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.

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

Comments

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);
       });
    });
 });

2 Comments

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.
That's because your doing this return { values }; What you should be doing is return values;
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
   })

1 Comment

TypeError: v.then is not a function at localhost/services/prefsService.js:27:13 at Array.forEach (native)

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.