【问题标题】:Problems with parsing a JSON Array after retrieving from firebase when using ionic-selectable使用离子可选时从 Firebase 检索后解析 JSON 数组的问题
【发布时间】:2019-03-15 17:58:46
【问题描述】:

我目前正在使用 Ionic 3 和 Firebase 开发应用程序。我正在使用 ionic-selectable(您可以看到我的 stackblitz here)让用户从我的 firebase 数据库数组中选择一个选项并将所选选项返回给用户的 id。

除了 ionic-selectable 没有从 firebase 读取检索到的数组之外,我已经完成了所有工作。

我正在使用以下代码检索数组:

    this.itemsRefdiag = afg.list('medicalhx');
    this.items = this.itemsRefdiag.snapshotChanges().map(changes => {
        return changes.map(c => ({ ...c.payload.val() }));
    });
    const dgRef = this.afg.database.ref();
    dgRef.child('medicalhx').orderByChild('name').on('value', snapshot => { this.snapshot2 = JSON.stringify(snapshot).replace(/"[0-9]":{"name":|{|}/g, ""); })

我的 console.log 结果:

"Hyperthyroidism","Hypothyroidism","Diabetes Type 1","Diabetes Type 2"

但是,当对private diagnoses: Diagnosis[] = [this.snapshot2] 使用离子可选时,我得到“未定义”选项。但是,当我手动输入private diagnoses: Diagnosis[] = ["Hyperthyroidism","Hypothyroidism","Diabetes Type 1","Diabetes Type 2"] 时,它可以工作。我还尝试使用以下代码解析 JSON 数组:

this.itemsRefdiag = afg.list('medicalhx');
    this.items = this.itemsRefdiag.snapshotChanges().map(changes => {
        return changes.map(c => ({ ...c.payload.val() }));
    });
const dbRef = this.afg.database.ref();
dbRef.child('medicalhx').orderByChild('name').on('value', snapshot =>
{     let snapshot3 = JSON.stringify(snapshot).replace(/"}/g, `"`);
let snapshot4 = snapshot3.replace(/"[0-9]":{"name":|{|}|"/g, "");
this.snapshot2 = snapshot4.split(",");
  });

我的 console.log 生成一个带有单独字符串的对象(一个数组):

["Hyperthyroidism","Hypothyroidism","Diabetes Type 1","Diabetes Type 2"]

但是,ionic-selectable 似乎仍然没有读取它,并且我收到未定义的错误。关于我可能对数组做错了什么有什么想法吗?

编辑

实际上第二次确实有效,但是第一次弹出控制台错误,我相信这是因为它没有等待数组结果第一次弹出。有没有办法在数组加载之前添加等待时间?

【问题讨论】:

    标签: arrays json firebase ionic-framework ionic3


    【解决方案1】:

    问题中列出的将其转换为真实数组的第二个代码有效,但需要加载时间,因此会弹出控制台错误。我设法通过按照 stackblitz 实现异步搜索来解决加载时间问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-22
      • 2017-04-07
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多