【问题标题】:Saving data to the firebase database将数据保存到 firebase 数据库
【发布时间】:2016-08-31 14:00:32
【问题描述】:

我正在尝试使用 firebase 并将数据保存到其中。我正在阅读这些文档https://firebase.google.com/docs/database/rest/save-data 但我对如何使用curl 命令感到困惑。

我有一个 .json 文件,我也有这个 javascript 文件,其中包含以下方法:

submitToDB.addEventListener('click', function() {
      if(document.getElementById('Hannah').checked){
        liked.set(Hannah.value)
        console.log('checked')
      } else {
        console.log('not checked')
      }
    });

Json 文件:

{
  "users": {
    "alanisawesome": {
      "date_of_birth": "June 23, 1912",
      "full_name": "Alan Turing"
    }
  }
}

这将检查是否喜欢 Hannah 复选框,然后将其添加到数据库中。但是,我无法从文档中解决如何添加大量 JSON 以及如何将用户输入的 JSON 转换为我想要的形式提交。

如果不清楚可以回答任何问题。

【问题讨论】:

标签: javascript json firebase firebase-realtime-database


【解决方案1】:

嘿,如果您以前从未使用过 curl,它可能会令人困惑。无论如何,这是我在 typescript 中使用的解决方案,但它也应该适用于 javascript。

假设你有:

export class SubmitData {
      _listPath:FirebaseListObservable<any>;
      constructor(firebase:Firebase) {
          const path = `/users/`;
          //pass the path here
          this._listPath = _firebase.database.list(path);
      }

 submitData() {
    //Magic happens here. This works. Test it with my test app.
    //Array object. Creates a user or users
    let data = [
          {
            alanisawesome: "Genius",
            date_of_birth: "June 23, 1912",
            full_name: "Alan Turing"
          },
          {
            fathersofSilicon: "Silicon Icon",
            date_of_birth: "October 13, 1956",
            full_name: "Marc Regis Hannah"
          }

       ]
     //This is how the push method works.
     return this._listPath.push(data); 

    } 
  }

【讨论】:

  • 提交数据最简单的方法是使用我创建的数组对象的版本。当您发现您希望如何在 Firebase 中真正格式化您的数据时,这将使您快速启动并运行。查看您的 Firebase 数据的结构。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-09
相关资源
最近更新 更多