Adding custom data to firebase database

时间:2017-08-05 12:04:01

标签: javascript firebase react-native firebase-realtime-database firebase-authentication

I have a signup screen in my app which contains few things:

full name,email and password. I want to add the users full name and in the future a phone number (for example) to the realtime database. How can I do so?

I'm using onCreate from firebase cloud functions to add a photoUrl and email to the database, but couldn't find a way to add my own properties such phone number,city,full name etc.

In the end result, I want to have a registration form with all of the above properties. How can I do so?

This is my onCreate method:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);
const ref = admin.database().ref()

exports.createUserAccount = functions.auth.user().onCreate(event=>{
  const uid = event.data.uid
  const email = event.data.email
  const photoUrl = event.data.photoUrl || 'https://vignette1.wikia.nocookie.net/paulblartmallcop/images/9/9c/Person-placeholder-male.jpg/revision/latest?cb=20120708210100'

  const newUserRef = ref.child(`/users/${uid}`)
  return newUserRef.set({
    photoUrl: photoUrl,
    email: email,
  })
});

How can I add more properties to the user?

1 个答案:

答案 0 :(得分:0)

If you are looking to store your user details in firebase, use firebase database instead. Try this.

{
  "userList": {
    "JRHTHaIsjNPLXOQivY": {
      "fullName": "UmarZaii",
      "phoneNumber": "0123456789"
    },
    "JRHTHaKuTFIhnj02kE": {
      "fullName": "JohnDoe",
      "phoneNumber": "0123456789"
    }
  }
}

You have to create a function that saves your data to the database after you successfully created the account.

Make sure that you save all the details inside userID.

For more information on how to save data to firebase database check Firebase Documentation.