简体   繁体   中英

Saving data to the firebase database

I am trying to use firebase and saving data to it. I am reading these docs https://firebase.google.com/docs/database/rest/save-data but I am getting confused as how to use the curl command.

I have a .json file and I also have this javascript file with the following method in:

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

Json file:

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

This will check if the Hannah checkbox is liked but then add it to the database. However, I cant work out from the docs how to add large amounts of JSON and how to turn the user inputted into JSON and then submit in the form I want.

If unclear can answer any questions.

Hey the curl can be confusing if you never used it before. Regardless, here is a solution that I use in typescript but it should work with javascript as well.

Assuming you have:

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

    } 
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM