firebase / firebase/firebase-js-sdk

onlyOnce: true fails to work on onChildAdded and onChildRemoved

Open
#7,842 0 comments 0 reactions 0 assignees View on GitHub
api: database needs-attention question
Dominant language
TypeScript
Stars
5.1k
Forks
1k
Avg merge
2d 21h
Merged PRs (30d)
37

Description

### Operating System

macOS

### Browser Version

Safari 16.5.2

### Firebase SDK Version

10.7.0

### Firebase SDK Product:

Database

### Describe your project's tooling

index.html with source tag. Just javascript.

### Describe the problem

The [onlyOnce](https://firebase.google.com/docs/reference/js/database.listenoptions.md#listenoptions_interface) option fails to work on [onChildAdded](https://firebase.google.com/docs/reference/js/database.md#onchildadded_3) and [onChildRemoved](https://firebase.google.com/docs/reference/js/database.md#onchildremoved_3). I couldn't test onChildMoved though.

### Steps and code to reproduce issue

```
onChildRemoved(ref(db, "posts"), snapshot => {
console.log("==onChildRemoved==");
console.log(snapshot.val());
}, error => console.error(error), { onlyOnce : true });

onValue(ref(db, "posts"), snapshot => {
console.log("==onValue==");
console.log(snapshot.val());
}, error => console.error(error), { onlyOnce: true });

onChildMoved(ref(db, "posts"), snapshot => {
console.log("==onChildMoved==");
console.log(snapshot.val());
}, error => console.error(error), { onlyOnce: true });

onChildChanged(ref(db, "posts"), snapshot => {
console.log("==onChildChanged==");
console.log(snapshot.val());
}, error => console.error(error), { onlyOnce: true });

onChildAdded(ref(db, "posts"), snapshot => {
console.log("==onChildAdded==");
console.log(snapshot.val());
}, error => console.error(error), { onlyOnce: true });

set(ref(db), {
"posts": {
"ts-functions": {
"metrics": {
"views": 1200000,
"likes": 251000,
"shares": 100,
},
"title": "Why you should use TypeScript for writing Cloud Functions",
"author": "Doug",
},
"android-arch-3": {
"metrics": {
"views": 900000,
"likes": 117000,
"shares": 144,
},
"title": "Using Android Architecture Components with Firebase Realtime Database (Part 3)",
"author": "Doug",
}
}});
```

By running this example, onChildAdded gets triggered twice. Then, you can just delete the data from the database and the onChildRemoved will trigger twice. I don't know how to test the onChildMoved. Maybe onChildMoved just triggers once, maybe not, but it's worth looking into as well. onValue and onChildChanged triggers just once as expected.

Contributor guide

Open the contributing guide

Research direction

Reproduce the behavior from index.html using the supplied onChildAdded and onChildRemoved example, comparing them with onValue and onChildChanged, which already fire once. Trace the Firebase Realtime Database listener handling for onlyOnce, then verify that child-added, child-removed, and child-moved listeners each fire once.

Written by the indexing model from the issue text.

Assessment

Tech stack
firebase, javascript, typescript
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.