简体   繁体   中英

Using firebase cloud functions to listen for changes in the background

My DB schema is as follows:

"Works" : {
    "-LLiIlsIS1XJonGRa8j6" : {
        "acceptedDate" : {
        },
        "answers" : {
        },
        "category" : "matematyka",
            "createDate" : {
        },
        "creatorID" : "KjxVHwUirhUwHRbBDhMCRoHliMQ2",
            "finishDate" : {
        },
        "firebaseKey" : "-LLiIlsIS1XJonGRa8j6",
            "level" : "Liceum",
            "number" : 1,
            "pointAmount" : 8,
            "pointBoost" : 0,
            "rated" : 1,
            "reported" : false,
            "solverID" : "XKUNXPozOsMM3sgQXY2F5iVMAkZ2",
            "state" : "Completed",
            "taskAmount" : 1,
            "workText" : "Czy pierwiastek z liczby parzystej zawsze jest liczba parzysta?"
    },
    "-LLiKGP3Zq1uX3ugRzSF" : {
        "acceptedDate" : {
        },
        "answers" : {
        }
    },
    "available" : false,
        "boostAmount" : 0,
        "category" : "matematyka",
        "createDate" : {
    },
    "creatorID" : "KjxVHwUirhUwHRbBDhMCRoHliMQ2",
        "finishDate" : {
    },
    "firebaseKey" : "-LLiKGP3Zq1uX3ugRzSF",
        "level" : "Liceum",
        "number" : 2,
        "pointAmount" : 8,
        "pointBoost" : 0,
        "rated" : 1,
        "reported" : false,
        "solverID" : "XKUNXPozOsMM3sgQXY2F5iVMAkZ2",
        "state" : "Completed",
        "taskAmount" : 1,
        "workText" : "Czy pierwiastek z liczby ujemnej zawsze jest liczba ujemna?"
}

So I want the cloud function to fire up each time a new object is pushed to "Works". So far I have this code:

export const onWorkAddition = functions.database.ref('Works').onCreate(snap => {
    console.log('new work created');
    console.log(snap.val());
});

Unfortunately this code doesn't log anything. What am I missing? I am new to cloud functions so if I'm missing something important let me know.

I needed to do it like this

export const onWorkAddition = functions.database.ref('/Works/{workId}').onCreate((snapshot, context) => {
    console.log(context.params.workId);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM