Javascript 获取更新文档的名称作为变量

Javascript 获取更新文档的名称作为变量,javascript,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Firebase,Google Cloud Firestore,Google Cloud Functions,我想编写一个云函数,用于侦听特定集合中所有文档的更新,并对已更新的文档进行处理。这意味着应该有一种方法将文档名作为变量引用,而不是显式引用。现在,我可以发送一个特定文档的通知,如下所示: exports.onRiskUpdate = functions.firestore.document('users/45wfho9').onUpdate((_change: any) => { const payload = { notification: {

我想编写一个云函数,用于侦听特定集合中所有文档的更新,并对已更新的文档进行处理。这意味着应该有一种方法将文档名作为变量引用,而不是显式引用。现在,我可以发送一个特定文档的通知,如下所示:

exports.onRiskUpdate = functions.firestore.document('users/45wfho9').onUpdate((_change: any) => {
    const payload = {
        notification: {
            title: "Notification title",
            body: "Notification body",
        },
    };
    return admin.messaging().sendToTopic("45wfho9", payload);
});
我希望能够为任何用户做到这一点,而不仅仅是id为45wfho9的用户。您可以使用通配符:

exports.onRiskUpdate = functions.firestore.document('users/{userId}').onUpdate((change, context) => {
    const userId = context.params.userId;
    const payload = {
        notification: {
            title: "Notification title",
            body: "Notification body",
        },
    };
    return admin.messaging().sendToTopic(userId, payload);
});
这将侦听“用户”集合中所有文档的更新,然后您可以使用
context.params.userId
获取文档id

您可以使用通配符:

exports.onRiskUpdate = functions.firestore.document('users/{userId}').onUpdate((change, context) => {
    const userId = context.params.userId;
    const payload = {
        notification: {
            title: "Notification title",
            body: "Notification body",
        },
    };
    return admin.messaging().sendToTopic(userId, payload);
});
这将侦听“用户”集合中所有文档的更新,然后您可以使用
context.params.userId
获取文档id


谢谢!可以用适当的代码替换return语句中的字符串“45wfho9”吗?谢谢!可以用适当的代码替换return语句中的字符串“45wfho9”吗?