ewizard
ewizard

Reputation: 2862

Ionic - can't push object with a variable key

I am trying to create an array of phone numbers to use with the ionic native SMS package. It is fairly simple code:

 let array;
 let promises_array = [];
  this.storage.get('username').then((val) => {
    console.log('in storage');
    this.follow = this.af.list('/profiles/stylists/' + val + "/followers");
    this.subscription = this.follow.subscribe(items => items.forEach(item => {
      promises_array.push(new Promise((resolve, reject) => {
        console.log(JSON.stringify(item) + " item item item");
        let arr = Object.keys(item);
        console.log(item[arr[0]] + "    type followers");
        array.push(item[arr[0]]);    ;
        resolve();
      }))
    }));

    Promise.all(promises_array).then(() => {
      let month1 = date.getUTCMonth;
      let date1 = date.getUTCDate;


        this.sms.send(array, val + " just opened up a spot at " + time + " on " + month1 + " " + date1 + "!")
          .catch(e => { console.log(JSON.stringify(e))});
    })
   boool = false;
 })

The error I get (it is a runtime error that appears in the app) -

Uncaught (in promise): TypeError undefined is not an object (evaluating 'array_1.push')

I have confirmed that array_1.push corresponds to the line:

array.push(item[arr[0]]);

It is saying that item[arr[0]] is undefined, but the console output is:

[10:54:49]  console.log: in storage 
[10:54:49]  console.log: {"V":"7818646634"} item item item 
[10:54:49]  console.log: 7818646634 type followers 
[10:54:52]  console.log: "Message cancelled." 

The third line shows that it recognizes what item[arr[0]] is, it just thinks its undefined when it goes to push it. Any help would be great, thanks.

Upvotes: 0

Views: 220

Answers (1)

iamalismith
iamalismith

Reputation: 1571

You are not defining the array variable as an array, so it doesn't know it can be pushed to.

let array;

you should do:

let array = [];

Upvotes: 1

Related Questions