使用Firebase云功能侦听后台的更改

时间:2018-09-19 12:31:40

标签: javascript typescript firebase firebase-realtime-database google-cloud-functions

我的数据库架构如下:

"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?"
}

因此,我希望每次将新对象推送到“ Works”时都启动云功能。到目前为止,我有以下代码:

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

不幸的是,这段代码没有记录任何内容。我想念什么?我是云功能的新手,所以如果我错过了重要的事情,请告诉我。

1 个答案:

答案 0 :(得分:2)

我需要这样做

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