【问题标题】:Ionic - can't push object with a variable key离子 - 无法使用可变键推送对象
【发布时间】:2018-04-18 20:38:41
【问题描述】:

我正在尝试创建一个电话号码数组以与离子原生 SMS 包一起使用。这是相当简单的代码:

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

我得到的错误(它是应用程序中出现的运行时错误) -

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

我已经确认array_1.push对应的行:

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

是说item[arr[0]]是未定义的,但是控制台输出是:

[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." 

第三行表明它识别出item[arr[0]] 是什么,它只是认为它是未定义的,当它去推送它时。任何帮助都会很棒,谢谢。

【问题讨论】:

    标签: javascript arrays angular typescript ionic-framework


    【解决方案1】:

    您没有将array 变量定义为数组,因此它不知道可以将其推送到。

    let array;

    你应该这样做:

    let array = [];

    【讨论】:

    • 我们都去过那里:)
    猜你喜欢
    • 1970-01-01
    • 2019-07-02
    • 2022-10-06
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多