JavaScript: Firebase Realtime Database write Data but Variable in dictionary is not recognized Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of JavaScript: Firebase Realtime Database write Data but Variable in dictionary is not recognized without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I want to write my Data in a Firebase Realtime Database, but the problem is that the first value of the child should have the value of a variable but the variable is not recognized at variable. It should look like this:

In the child it displays two variables

so I tried:

let id = chatid + "::::" + username;
let input = document.getElementById('input').value;
 
firebase.database().ref('chat/'+ chatid + "/chat").set({
   id: input
})

but the id variable is not recognized as a variable, thats why in the firebase console it looks like this:

Instead of the value variable, the name of variable "id" is displayed to me

How to fix that?

Answer

You need to do as follows:

let id = chatid + "::::" + username;
let input = document.getElementById('input').value;

firebase
    .database()
    .ref('chat/' + chatid + '/chat/' + id)
    .set(input);

or as follows:

  firebase
    .database()
    .ref('chat/' + chatid + '/chat')
    .set({
      [id]: input,
    });

More detail on the square brackets approach in this SO answer.

We are here to answer your question about JavaScript: Firebase Realtime Database write Data but Variable in dictionary is not recognized - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji