Skip to content

Commit 554bf36

Browse files
authored
Add RTDB get() snippet (firebase#133)
1 parent e70c313 commit 554bf36

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

database-next/read-and-write.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,19 @@ function toggleStar_wrapped() {
161161
// [END rtdb_social_star_transaction]
162162
}
163163

164+
function readOnceWithGet(userId) {
165+
// [START rtdb_read_once_get]
166+
const { getDatabase, ref, child, get } = require("firebase/database");
167+
168+
const dbRef = ref(getDatabase(firebaseApp));
169+
get(child(dbRef, `users/${userId}`)).then((snapshot) => {
170+
if (snapshot.exists()) {
171+
console.log(snapshot.val());
172+
} else {
173+
console.log("No data available");
174+
}
175+
}).catch((error) => {
176+
console.error(error);
177+
});
178+
// [END rtdb_read_once_get]
179+
}

database/read-and-write.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,18 @@ function toggleStar(postRef, uid) {
123123
});
124124
}
125125
// [END rtdb_social_star_transaction]
126+
127+
function readOnceWithGet(userId) {
128+
// [START rtdb_read_once_get]
129+
const dbRef = firebase.database().ref();
130+
dbRef.child("users").child(userId).get().then((snapshot) => {
131+
if (snapshot.exists()) {
132+
console.log(snapshot.val());
133+
} else {
134+
console.log("No data available");
135+
}
136+
}).catch((error) => {
137+
console.error(error);
138+
});
139+
// [END rtdb_read_once_get]
140+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./database-next/read-and-write.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START rtdb_read_once_get_modular]
7+
import { getDatabase, ref, child, get } from "firebase/database";
8+
9+
const dbRef = ref(getDatabase(firebaseApp));
10+
get(child(dbRef, `users/${userId}`)).then((snapshot) => {
11+
if (snapshot.exists()) {
12+
console.log(snapshot.val());
13+
} else {
14+
console.log("No data available");
15+
}
16+
}).catch((error) => {
17+
console.error(error);
18+
});
19+
// [END rtdb_read_once_get_modular]

0 commit comments

Comments
 (0)