2

I want to know how the retrieve "good" array from angular function. I have this function in angular:

app.run(function($rootScope,Communications,$http,$filter) {

$rootScope.getCommunication = 
function(object_type,val,id,isType,isSendSms,getNotes){

  var array = {};
  var newArr = [];

  // getting data from mysql data 
  var myVals = Communications.find({
        filter: {
          where: {
            and : [{
              communications_type_code : val
            },{
              object_id : id
            },{
              object_type : object_type
            }]
          }
        } 
      }).$promise
        .then(function(data) {

          for(var ind=0; ind<data.length; ind++){
            array['address_type'] = data[ind].address_type;
            array['contact_value'] = data[ind].contact_value;
            array['send_sms'] = data[ind].send_sms;
          }

          newArr.push(array);
           return newArr;
        });
  return newArr;
};
});

When I call to the function in Angular controller like this:

var arr = $rootScope.getCommunication(2,3,$id);
console.log(arr);

I am getting in the console something like this:

enter image description here

When I call to arr[0] I get undefined. How can i recieve this data?

2 Answers 2

1

You need to return the promise, currently you are returning the array before it is updated. Async works by running all the synchronous code first then running anything async.

var arr = []; // runs first
promise.then(function(result){arr = result}) // runs third.
return arr; // runs second 

You need to change your code to return the promise. However this also means your calling code has to handle async code.

function asyncFunc() {
 return promise.then(function(result){return result});
}

asyncFunc().then(function(result) {console.log(result)}) // output is the value of result.

In the context of the code you gave above

app.run(function($rootScope,Communications,$http,$filter) {

$rootScope.getCommunication = 
function(object_type,val,id,isType,isSendSms,getNotes){
  // getting data from mysql data 
  return Communications.find({
        filter: {
          where: {
            and : [{
              communications_type_code : val
            },{
              object_id : id
            },{
              object_type : object_type
            }]
          }
        } 
      }).$promise
        .then(function(data) {
          var array = {};
          var newArray = [];
          for(var ind=0; ind<data.length; ind++){
            array['address_type'] = data[ind].address_type;
            array['contact_value'] = data[ind].contact_value;
            array['send_sms'] = data[ind].send_sms;
          }

          newArr.push(array);
           return newArr;
        });
};
});

and the calling function:

var arr = $rootScope.getCommunication(2,3,$id)
                    .then(function(arr){console.log(arr)})
Sign up to request clarification or add additional context in comments.

1 Comment

I amg getting angular.js:12520 TypeError: Cannot read property 'then' of undefined from the call of the function
0

You can use callback in following way

app.run(function($rootScope,Communications,$http,$filter) {

$rootScope.getCommunication = 
function(object_type,val,id,isType,isSendSms,getNotes, callback){

  var array = {};
  var newArr = [];

  // getting data from mysql data 
  var myVals = Communications.find({
        filter: {
          where: {
            and : [{
              communications_type_code : val
            },{
              object_id : id
            },{
              object_type : object_type
            }]
          }
        } 
      }).$promise
        .then(function(data) {

          for(var ind=0; ind<data.length; ind++){
            array['address_type'] = data[ind].address_type;
            array['contact_value'] = data[ind].contact_value;
            array['send_sms'] = data[ind].send_sms;
          }

          newArr.push(array);

           //return using callback
           return callback(newArr);

        });

  //return using callback
  return callback(newArr);

};
});

And use callback to access result

$rootScope.getCommunication(2,3,$id, function(result){
  var arr = result
})

4 Comments

I am getting undefined callback
make identical/equal parameter when calling function getCommunication
I still get an error TypeError: callback is not a function
check my updated answer and fit the function where you need , the way of callback function is correct.

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.