0
votes

This is my current Firebase database configuration where I want to use username as key and CurrentUser.ID as the value.

I tried to use string interpolation but I got some error.

function updateExistingUserRoot(username) {
  const { currentUser } = firebase.auth();
  return (dispatch) => {
    firebase.database().ref(`/ExistingUser`).push({
      `${username}`: currentUser.uid
    })
  }
}

I understand firebase generates an unique key every time data is being pushed but I would like to stay with the current configuration.

Update 1

I have changed to using set but the error persists.

function updateExistingUserRoot(username) {
  const { currentUser } = firebase.auth();
  firebase.database().ref(`/ExistingUser`).set({
    `${username}`: currentUser.uid
  });
}

The error: expected property assignment.

3

3 Answers

0
votes

For the configuration you want, you should rather be using set or update methods to save or update your data.

firebase.database().ref(`/ExistingUser`).set({
      `${username}`: currentUser.uid
})

This way, your data gets saved in the username node which will be mapped with the user's uid.

Hope that helps!

0
votes

If you want to create a user with a specific key, you should remove the push method (that creates a unique key) and use something like:

firebase.database().ref(`/ExistingUser` + userKey).set({
    username: value...
  });

Everything in Firebase is an url.

https://testxxx.firebaseio.com/xxx/ExistingUser

If you want to create a user with a key of KEY1 as a child at this location you would have:

https://testxxx.firebaseio.com/xxx/ExistingUser/KEY1
0
votes

The answer given by the two gentlemen are correct but I had to tweaked a little bit to get it working.

Here's the code

firebase.database().ref(`/ExistingUser/${username}`).set({
   userID: currentUser.uid
 }).then(() => console.log('Set existing user done'))
   .catch((error) => console.log(error.message))