Add a new pair of key values โ€‹โ€‹to existing Firebase

This may be a fairly simple question, but so far I canโ€™t find the answer to my problem on the Internet after much searching. I have a firebase web application where the data structure is pretty simple. It is initially empty, for example:

fireRef { } 

I want to be able to add pairs of key values โ€‹โ€‹where the key is created by the user and the value is just text. For example, the user enters his name as a key, and the value as his age. Then I want to send this data to the server and now firebase looks like this:

 fireRef { John : 25, } 

I can do this one addition:

 var name = getUserName(); var age = getUserAge(); var node = {}; node[name] = age; fireRef.set(node); 

However, I want several people to be able to do this. When I try to add a new person to the server, the old โ€œJohn: 25โ€ pair turns red and disappears, leaving only a pair of new keys.

How can I keep around and maintain a data set of a pair of keys, pairs of values?

+6
source share
2 answers

A unique identifier in firebase is generated when we push .

For instance:

 var fireRef = new Firebase('https://<CHANGE_APP_NAME>.firebaseio.com/fireRef'); var newUserRef = fireRef.push(); newUserRef.set({ 'name': 'fred', 'age': '32' }); 

Another way is to set child elements directly:

 var fireRef = new Firebase('https://<CHANGE_APP_NAME>.firebaseio.com/fireRef'); fireRef.child(1).set({'name':'user2','age':'34'}); fireRef.child(2).set({'name':'user3','age':'24'}); 
+9
source

@ user3749797, I was confused with this exact problem.

@learningloop offers a good solution, since it allows you to perform the task of adding data to your firebase, but it is possible to add a new pair k, v (name, age) to one associative JSON array, rather than clicking on an array of associative arrays. In fact, @learningloop sees:

[{ name: steve, age: 34 }, { name: mary, age: 22 }]

Perhaps his path is better, but you and I were looking for:

{ steve: 34, mary: 22 }

I managed to add k, v pairs to this list with

var fireRef = new Firebase('https://<CHANGE_APP_NAME>.firebaseio.com/fireRef'); fireRef.update({ 'jobe': '33'});

Yielding

{ steve: 34, mary: 22, jobe: 33 }

In my firebase.

Full documentation on saving to firebase [here]

+2
source

Source: https://habr.com/ru/post/986395/


All Articles