Adding a new value in a node firebase
To add a new value to a node in Firebase Realtime Database or Cloud Firestore, you can follow these steps:
Firebase Realtime Database:
-
First, you need to get a reference to the node where you want to add the new value. You can do this using the
ref()
method:var db = firebase.database(); var newNode = db.ref('path/to/node');
Replace
'path/to/node'
with the actual path to the node where you want to add the new value. -
Then, use the
push()
method to add a new child node to the node:newNode.push({ key: 'newValue' });
This will add a new child node with the key
'newValue'
to the node.
Cloud Firestore:
-
First, you need to get a reference to the node where you want to add the new value. You can do this using the
collection()
anddoc()
methods:var db = firebase.firestore(); var newNode = db.collection('path/to/collection').doc('path/to/document');
Replace
'path/to/collection'
and'path/to/document'
with the actual path to the node where you want to add the new value. -
Then, use the
set()
method to add a new document to the node:newNode.set({ key: 'newValue' });
This will add a new document with the key
'newValue'
to the node.
Example Code:
Here is an example code snippet that adds a new value to a node in Firebase Realtime Database:
var db = firebase.database();
var newNode = db.ref('users').push({
name: 'John Doe',
age: 30
});
And here is an example code snippet that adds a new value to a node in Cloud Firestore:
var db = firebase.firestore();
var newNode = db.collection('users').doc().set({
name: 'John Doe',
age: 30
});
Note that in Cloud Firestore, you need to use the doc()
method to create a new document reference, and then use the set()
method to add the new document.