Skip to content

Commit 42865f2

Browse files
chore(update-plugins): Mon Sep 2 08:05:12 UTC 2024
1 parent 1b6c410 commit 42865f2

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

content/plugins/firebase-database.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,14 @@ If you are listening to a node with many children, only listening to data you ca
169169
```ts
170170
import { firebase } from '@nativescript/firebase-core'
171171

172-
const onChildAdd = firebase()
173-
.database()
174-
.ref('/users')
175-
.on('child_added', (snapshot) => {
176-
console.log('A new node has been added', snapshot.val())
177-
})
172+
const ref = firebase().database().ref('/users')
173+
174+
const onChildAdd = ref.on('child_added', (snapshot) => {
175+
console.log('A new node has been added', snapshot.val())
176+
})
178177

179178
// Stop listening for updates when no longer required
180-
firebase().database().ref('/users').off('child_added', onChildAdd)
179+
ref.off('child_added', onChildAdd)
181180
```
182181

183182
### Remove a reference event listener
@@ -187,17 +186,22 @@ To unsubscribe from an event, call the `off` method on the reference passing it
187186
```ts
188187
import { firebase } from '@nativescript/firebase-core'
189188

190-
const onValueChange = firebase()
191-
.database()
192-
.ref(`/users/${userId}`)
193-
.on('value', (snapshot) => {
194-
console.log('User data: ', snapshot.val())
195-
})
189+
const ref = firebase().database().ref(`/users/${userId}`)
190+
191+
const onValueChange = ref.on('value', (snapshot) => {
192+
console.log('User data: ', snapshot.val())
193+
})
196194

197195
// Stop listening for updates when no longer required
198-
firebase().database().ref(`/users/${userId}`).off('value', onValueChange)
196+
ref.off('value', onValueChange)
199197
```
200198

199+
:::tip Note
200+
201+
The `off` method requires the same `ref` as specified on the corresponding `on` method. The event handler specified in the `on` method must be unique. If a common event handler is used for multiple events, an anonymous function can be used to invoke the common handler.
202+
203+
:::
204+
201205
### Data querying
202206

203207
Realtime Database provides support for basic querying of your data. When a reference node contains children, you can both order & limit the returned results.

0 commit comments

Comments
 (0)