@@ -4,41 +4,41 @@ function readValue() {
4
4
// [START rtdb_read_value]
5
5
// Get a database reference to our posts
6
6
const db = admin . database ( ) ;
7
- const ref = db . ref ( " server/saving-data/fireblog/posts" ) ;
7
+ const ref = db . ref ( ' server/saving-data/fireblog/posts' ) ;
8
8
9
9
// Attach an asynchronous callback to read the data at our posts reference
10
- ref . on ( " value" , ( snapshot ) => {
10
+ ref . on ( ' value' , ( snapshot ) => {
11
11
console . log ( snapshot . val ( ) ) ;
12
12
} , ( errorObject ) => {
13
- console . log ( " The read failed: " + errorObject . name ) ;
13
+ console . log ( ' The read failed: ' + errorObject . name ) ;
14
14
} ) ;
15
15
// [END rtdb_read_value]
16
16
}
17
17
18
18
function childAdded ( ) {
19
19
const db = admin . database ( ) ;
20
- const ref = db . ref ( " server/saving-data/fireblog/posts" ) ;
20
+ const ref = db . ref ( ' server/saving-data/fireblog/posts' ) ;
21
21
22
22
// [START rtdb_child_added]
23
23
// Retrieve new posts as they are added to our database
24
- ref . on ( " child_added" , ( snapshot , prevChildKey ) => {
24
+ ref . on ( ' child_added' , ( snapshot , prevChildKey ) => {
25
25
const newPost = snapshot . val ( ) ;
26
- console . log ( " Author: " + newPost . author ) ;
27
- console . log ( " Title: " + newPost . title ) ;
28
- console . log ( " Previous Post ID: " + prevChildKey ) ;
26
+ console . log ( ' Author: ' + newPost . author ) ;
27
+ console . log ( ' Title: ' + newPost . title ) ;
28
+ console . log ( ' Previous Post ID: ' + prevChildKey ) ;
29
29
} ) ;
30
30
// [END rtdb_child_added]
31
31
}
32
32
33
33
function childChanged ( ) {
34
34
const db = admin . database ( ) ;
35
- const ref = db . ref ( " server/saving-data/fireblog/posts" ) ;
35
+ const ref = db . ref ( ' server/saving-data/fireblog/posts' ) ;
36
36
37
37
// [START rtdb_child_changed]
38
38
// Get the data on a post that has changed
39
- ref . on ( " child_changed" , ( snapshot ) => {
39
+ ref . on ( ' child_changed' , ( snapshot ) => {
40
40
const changedPost = snapshot . val ( ) ;
41
- console . log ( " The updated post title is " + changedPost . title ) ;
41
+ console . log ( ' The updated post title is ' + changedPost . title ) ;
42
42
} ) ;
43
43
// [END rtdb_child_changed]
44
44
}
@@ -48,72 +48,72 @@ function childRemoved() {
48
48
49
49
// [START rtdb_child_removed]
50
50
// Get a reference to our posts
51
- const ref = db . ref ( " server/saving-data/fireblog/posts" ) ;
51
+ const ref = db . ref ( ' server/saving-data/fireblog/posts' ) ;
52
52
53
53
// Get the data on a post that has been removed
54
- ref . on ( " child_removed" , ( snapshot ) => {
54
+ ref . on ( ' child_removed' , ( snapshot ) => {
55
55
const deletedPost = snapshot . val ( ) ;
56
- console . log ( " The blog post titled '" + deletedPost . title + "' has been deleted" ) ;
56
+ console . log ( ' The blog post titled \'' + deletedPost . title + '\' has been deleted' ) ;
57
57
} ) ;
58
58
// [END rtdb_child_removed]
59
59
}
60
60
61
61
function eventGuarantees ( ) {
62
62
const db = admin . database ( ) ;
63
- const ref = db . ref ( " server/saving-data/fireblog/posts" ) ;
63
+ const ref = db . ref ( ' server/saving-data/fireblog/posts' ) ;
64
64
65
65
// [START rtdb_event_guarantees]
66
66
let count = 0 ;
67
67
68
- ref . on ( " child_added" , ( snap ) => {
68
+ ref . on ( ' child_added' , ( snap ) => {
69
69
count ++ ;
70
- console . log ( " added:" , snap . key ) ;
70
+ console . log ( ' added:' , snap . key ) ;
71
71
} ) ;
72
72
73
73
// length will always equal count, since snap.val() will include every child_added event
74
74
// triggered before this point
75
- ref . once ( " value" , ( snap ) => {
76
- console . log ( " initial data loaded!" , snap . numChildren ( ) === count ) ;
75
+ ref . once ( ' value' , ( snap ) => {
76
+ console . log ( ' initial data loaded!' , snap . numChildren ( ) === count ) ;
77
77
} ) ;
78
78
// [END rtdb_event_guarantees]
79
79
}
80
80
81
81
function detatchCallbacks ( ) {
82
82
const db = admin . database ( ) ;
83
- const ref = db . ref ( " server/saving-data/fireblog/posts" ) ;
83
+ const ref = db . ref ( ' server/saving-data/fireblog/posts' ) ;
84
84
85
85
const originalCallback = ( ) => {
86
86
// ...
87
- }
87
+ } ;
88
88
89
89
// [START rtdb_detach_callbacks]
90
- ref . off ( " value" , originalCallback ) ;
90
+ ref . off ( ' value' , originalCallback ) ;
91
91
// [END rtdb_detach_callbacks]
92
92
}
93
93
94
94
function detatchCallbacksContext ( ) {
95
95
const db = admin . database ( ) ;
96
- const ref = db . ref ( " server/saving-data/fireblog/posts" ) ;
96
+ const ref = db . ref ( ' server/saving-data/fireblog/posts' ) ;
97
97
98
98
const originalCallback = ( ) => {
99
99
// ...
100
- }
100
+ } ;
101
101
102
102
/** {@type any } */
103
103
let ctx ;
104
104
105
105
// [START rtdb_detach_callbacks_context]
106
- ref . off ( " value" , originalCallback , ctx ) ;
106
+ ref . off ( ' value' , originalCallback , ctx ) ;
107
107
// [END rtdb_detach_callbacks_context]
108
108
}
109
109
110
110
function detachAllCallbacks ( ) {
111
111
const db = admin . database ( ) ;
112
- const ref = db . ref ( " server/saving-data/fireblog/posts" ) ;
112
+ const ref = db . ref ( ' server/saving-data/fireblog/posts' ) ;
113
113
114
114
// [START rtdb_detach_all_callbacks]
115
115
// Remove all value callbacks
116
- ref . off ( " value" ) ;
116
+ ref . off ( ' value' ) ;
117
117
118
118
// Remove all callbacks of any type
119
119
ref . off ( ) ;
@@ -122,10 +122,10 @@ function detachAllCallbacks() {
122
122
123
123
function readOnce ( ) {
124
124
const db = admin . database ( ) ;
125
- const ref = db . ref ( " server/saving-data/fireblog/posts" ) ;
125
+ const ref = db . ref ( ' server/saving-data/fireblog/posts' ) ;
126
126
127
127
// [START rtdb_read_once]
128
- ref . once ( " value" , ( data ) => {
128
+ ref . once ( ' value' , ( data ) => {
129
129
// do some stuff once
130
130
} ) ;
131
131
// [END rtdb_read_once]
@@ -135,10 +135,10 @@ function orderByChild() {
135
135
const db = admin . database ( ) ;
136
136
137
137
// [START rtdb_order_by_child]
138
- const ref = db . ref ( " dinosaurs" ) ;
138
+ const ref = db . ref ( ' dinosaurs' ) ;
139
139
140
- ref . orderByChild ( " height" ) . on ( " child_added" , ( snapshot ) => {
141
- console . log ( snapshot . key + " was " + snapshot . val ( ) . height + " meters tall" ) ;
140
+ ref . orderByChild ( ' height' ) . on ( ' child_added' , ( snapshot ) => {
141
+ console . log ( snapshot . key + ' was ' + snapshot . val ( ) . height + ' meters tall' ) ;
142
142
} ) ;
143
143
// [END rtdb_order_by_child]
144
144
}
@@ -147,9 +147,9 @@ function orderByChildNested() {
147
147
const db = admin . database ( ) ;
148
148
149
149
// [START rtdb_order_by_child_nested]
150
- const ref = db . ref ( " dinosaurs" ) ;
151
- ref . orderByChild ( " dimensions/height" ) . on ( " child_added" , ( snapshot ) => {
152
- console . log ( snapshot . key + " was " + snapshot . val ( ) . height + " meters tall" ) ;
150
+ const ref = db . ref ( ' dinosaurs' ) ;
151
+ ref . orderByChild ( ' dimensions/height' ) . on ( ' child_added' , ( snapshot ) => {
152
+ console . log ( snapshot . key + ' was ' + snapshot . val ( ) . height + ' meters tall' ) ;
153
153
} ) ;
154
154
// [END rtdb_order_by_child_nested]
155
155
}
@@ -158,8 +158,8 @@ function orderByKey() {
158
158
const db = admin . database ( ) ;
159
159
160
160
// [START rtdb_order_by_key]
161
- var ref = db . ref ( " dinosaurs" ) ;
162
- ref . orderByKey ( ) . on ( " child_added" , ( snapshot ) => {
161
+ var ref = db . ref ( ' dinosaurs' ) ;
162
+ ref . orderByKey ( ) . on ( ' child_added' , ( snapshot ) => {
163
163
console . log ( snapshot . key ) ;
164
164
} ) ;
165
165
// [END rtdb_order_by_key]
@@ -169,10 +169,10 @@ function orderByValue() {
169
169
const db = admin . database ( ) ;
170
170
171
171
// [START rtdb_order_by_value]
172
- const scoresRef = db . ref ( " scores" ) ;
173
- scoresRef . orderByValue ( ) . on ( " value" , ( snapshot ) => {
172
+ const scoresRef = db . ref ( ' scores' ) ;
173
+ scoresRef . orderByValue ( ) . on ( ' value' , ( snapshot ) => {
174
174
snapshot . forEach ( ( data ) => {
175
- console . log ( " The " + data . key + " dinosaur's score is " + data . val ( ) ) ;
175
+ console . log ( ' The ' + data . key + ' dinosaur\ 's score is ' + data . val ( ) ) ;
176
176
} ) ;
177
177
} ) ;
178
178
// [END rtdb_order_by_value]
@@ -182,8 +182,8 @@ function limitToLast() {
182
182
const db = admin . database ( ) ;
183
183
184
184
// [START rtdb_limit_to_last]
185
- const ref = db . ref ( " dinosaurs" ) ;
186
- ref . orderByChild ( " weight" ) . limitToLast ( 2 ) . on ( " child_added" , ( snapshot ) => {
185
+ const ref = db . ref ( ' dinosaurs' ) ;
186
+ ref . orderByChild ( ' weight' ) . limitToLast ( 2 ) . on ( ' child_added' , ( snapshot ) => {
187
187
console . log ( snapshot . key ) ;
188
188
} ) ;
189
189
// [END rtdb_limit_to_last]
@@ -193,8 +193,8 @@ function limitToFirst() {
193
193
const db = admin . database ( ) ;
194
194
195
195
// [START rtdb_limit_to_first]
196
- const ref = db . ref ( " dinosaurs" ) ;
197
- ref . orderByChild ( " height" ) . limitToFirst ( 2 ) . on ( " child_added" , ( snapshot ) => {
196
+ const ref = db . ref ( ' dinosaurs' ) ;
197
+ ref . orderByChild ( ' height' ) . limitToFirst ( 2 ) . on ( ' child_added' , ( snapshot ) => {
198
198
console . log ( snapshot . key ) ;
199
199
} ) ;
200
200
// [END rtdb_limit_to_first]
@@ -204,10 +204,10 @@ function limitOrderValue() {
204
204
const db = admin . database ( ) ;
205
205
206
206
// [START rtdb_limit_order_value]
207
- const scoresRef = db . ref ( " scores" ) ;
208
- scoresRef . orderByValue ( ) . limitToLast ( 3 ) . on ( " value" , ( snapshot ) => {
207
+ const scoresRef = db . ref ( ' scores' ) ;
208
+ scoresRef . orderByValue ( ) . limitToLast ( 3 ) . on ( ' value' , ( snapshot ) => {
209
209
snapshot . forEach ( ( data ) => {
210
- console . log ( " The " + data . key + " dinosaur's score is " + data . val ( ) ) ;
210
+ console . log ( ' The ' + data . key + ' dinosaur\ 's score is ' + data . val ( ) ) ;
211
211
} ) ;
212
212
} ) ;
213
213
// [END rtdb_limit_order_value]
@@ -217,8 +217,8 @@ function startAt() {
217
217
const db = admin . database ( ) ;
218
218
219
219
// [START rtdb_start_at]
220
- const ref = db . ref ( " dinosaurs" ) ;
221
- ref . orderByChild ( " height" ) . startAt ( 3 ) . on ( " child_added" , ( snapshot ) => {
220
+ const ref = db . ref ( ' dinosaurs' ) ;
221
+ ref . orderByChild ( ' height' ) . startAt ( 3 ) . on ( ' child_added' , ( snapshot ) => {
222
222
console . log ( snapshot . key ) ;
223
223
} ) ;
224
224
// [END rtdb_start_at]
@@ -227,8 +227,8 @@ function startAt() {
227
227
function endAt ( ) {
228
228
const db = admin . database ( ) ;
229
229
// [START rtdb_end_at]
230
- const ref = db . ref ( " dinosaurs" ) ;
231
- ref . orderByKey ( ) . endAt ( " pterodactyl" ) . on ( " child_added" , ( snapshot ) => {
230
+ const ref = db . ref ( ' dinosaurs' ) ;
231
+ ref . orderByKey ( ) . endAt ( ' pterodactyl' ) . on ( ' child_added' , ( snapshot ) => {
232
232
console . log ( snapshot . key ) ;
233
233
} ) ;
234
234
// [END rtdb_end_at]
@@ -238,8 +238,8 @@ function startAtEndAt() {
238
238
const db = admin . database ( ) ;
239
239
240
240
// [START rtdb_start_at_end_at]
241
- var ref = db . ref ( " dinosaurs" ) ;
242
- ref . orderByKey ( ) . startAt ( "b" ) . endAt ( " b\uf8ff" ) . on ( " child_added" , ( snapshot ) => {
241
+ var ref = db . ref ( ' dinosaurs' ) ;
242
+ ref . orderByKey ( ) . startAt ( 'b' ) . endAt ( ' b\uf8ff' ) . on ( ' child_added' , ( snapshot ) => {
243
243
console . log ( snapshot . key ) ;
244
244
} ) ;
245
245
// [END rtdb_start_at_end_at]
@@ -249,8 +249,8 @@ function equalTo() {
249
249
const db = admin . database ( ) ;
250
250
251
251
// [START rtdb_equal_to]
252
- const ref = db . ref ( " dinosaurs" ) ;
253
- ref . orderByChild ( " height" ) . equalTo ( 25 ) . on ( " child_added" , ( snapshot ) => {
252
+ const ref = db . ref ( ' dinosaurs' ) ;
253
+ ref . orderByChild ( ' height' ) . equalTo ( 25 ) . on ( ' child_added' , ( snapshot ) => {
254
254
console . log ( snapshot . key ) ;
255
255
} ) ;
256
256
// [END rtdb_equal_to]
@@ -260,22 +260,22 @@ function complexCombined() {
260
260
const db = admin . database ( ) ;
261
261
262
262
// [START rtdb_complex_combined]
263
- const ref = db . ref ( " dinosaurs" ) ;
264
- ref . child ( " stegosaurus" ) . child ( " height" ) . on ( " value" , ( stegosaurusHeightSnapshot ) => {
263
+ const ref = db . ref ( ' dinosaurs' ) ;
264
+ ref . child ( ' stegosaurus' ) . child ( ' height' ) . on ( ' value' , ( stegosaurusHeightSnapshot ) => {
265
265
const favoriteDinoHeight = stegosaurusHeightSnapshot . val ( ) ;
266
266
267
- const queryRef = ref . orderByChild ( " height" ) . endAt ( favoriteDinoHeight ) . limitToLast ( 2 )
268
- queryRef . on ( " value" , ( querySnapshot ) => {
267
+ const queryRef = ref . orderByChild ( ' height' ) . endAt ( favoriteDinoHeight ) . limitToLast ( 2 ) ;
268
+ queryRef . on ( ' value' , ( querySnapshot ) => {
269
269
if ( querySnapshot . numChildren ( ) === 2 ) {
270
270
// Data is ordered by increasing height, so we want the first entry
271
271
querySnapshot . forEach ( ( dinoSnapshot ) => {
272
- console . log ( " The dinosaur just shorter than the stegasaurus is " + dinoSnapshot . key ) ;
272
+ console . log ( ' The dinosaur just shorter than the stegasaurus is ' + dinoSnapshot . key ) ;
273
273
274
274
// Returning true means that we will only loop through the forEach() one time
275
275
return true ;
276
276
} ) ;
277
277
} else {
278
- console . log ( " The stegosaurus is the shortest dino" ) ;
278
+ console . log ( ' The stegosaurus is the shortest dino' ) ;
279
279
}
280
280
} ) ;
281
281
} ) ;
0 commit comments