0

This is the factory I use right now, I get 2 json objects and try to do a array of them then I cant use it in the textreplacer.

app.factory('myFactory', function ($http) {
  var service = {}
  var array =  [];
  var obj = [];


  service.getText = function () {
    return text =  $http.get('api/json_text').then(function(res){
        return res.data;
    });
  }
  service.getShortcuts = function () {
    return shortcuts = $http.get('api/json_shortcuts').then(function(res){
        return res.data;
    });
  }
  service.merchJson = function () {
    service.getText().then(function(text) {
      service.getShortcuts().then(function(shortcuts) {
        angular.forEach(text, function(value, key) {
          if(!angular.isUndefined(shortcuts[value])){
            array_value = shortcuts[value];
            obj[key] = array_value;
            array.push(obj[key]);
          }else{
            array_value = text[key].toString();
            obj[key] = array_value;
            array.push(obj[key]);
          }
        });

      });
    });
    return array;
  }
  service.textReplacer = function () {
      var array = service.merchJson();
      angular.forEach(array, function(value, key) {

      });
      console.log(array);

  }
  return service;
})

Is it something I done wrong or a bug because I can see it in console.log(array) without any problem.

2
  • your array is inside asynchronous callbacks, you can't return it like a normal variable. Once a Promise is always a Promise Commented Mar 7, 2018 at 15:40
  • Dont know how promise work:/ Commented Mar 7, 2018 at 15:52

1 Answer 1

0

try this

service.merchJson = function () {
    var result;
    return service.getText().then(function (text) {
            result = text;
            return service.getShortcuts();
        })
        .then(function (shortcuts) {
            angular.forEach(result, function (value, key) {
                if (!angular.isUndefined(shortcuts[value])) {
                    array_value = shortcuts[value];
                    obj[key] = array_value;
                    array.push(obj[key]);
                } else {
                    array_value = result[key].toString();
                    obj[key] = array_value;
                    array.push(obj[key]);
                }
            });
            return array;
        });
}


service.textReplacer = function () {
    service.merchJson().then(function (array ) {
        angular.forEach(array, function (value, key) {});
        console.log(array);
    });
}
Sign up to request clarification or add additional context in comments.

6 Comments

text is not within the scope for the second .then I think
so that it was from the controller now becouse it wasent a return there but i only get this now
f {$$state: {…}}$$state:
TypeError: Cannot read property 'toString' of undefined at editor.js:30
array_value = text[key].toString();
|

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.