Skip to content

onlyOnce: true fails to work on onChildAdded and onChildRemoved #7842

Open
@marcusx2

Description

@marcusx2

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 option fails to work on onChildAdded and onChildRemoved. 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions