0

我在这里尝试将云任务与云功能一起使用,这样当我收藏的某个字段设置为 true 时,任务会在 24 小时后自动将其设置为 false。但我不太了解使用云任务和云功能的一切,所以我真的需要一些帮助......

如何获取 Trigger onUpdate Cloud 函数的 userId 的值以发送到 Http 云函数中,以便我可以使用它来更新特定字段,即地图?

这是我的功能:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
const { CloudTasksClient } = require('@google-cloud/tasks');

admin.initializeApp()
    exports.onUpdateSavedPostsReUpdateItLater = functions.firestore
      .document("/saved/{userId}/savedPosts/{postId}")
      .onUpdate(async (change, context) => {
        const userId = context.params.userId;
        const postUpdate = change.after.data();
        const pStatus = postUpdate.prayers[userId]; // The field that i want to update


        updateAtSeconds = Date.now() / 1000 + 86400; //86400 equal to 24 hours, because i want my documents to be updated in the next 24 hours


        // Get the project ID from the FIREBASE_CONFIG env var
        const project = JSON.parse(process.env.FIREBASE_CONFIG!).projectId;
        const location = 'europe-west3';
        const queue = 'ijn-amen';

        const tasksClient = new CloudTasksClient();
        const queuePath: string =
          tasksClient.queuePath(project, location, queue);


        const url = 'https://${location}-${project}.cloudfunctions.net/firestoreTtlCallback';
        const docPath = change.ref.path;
        const payload: UpdateTaskPayload = { docPath };

        const task = {
          httpRequest: {
            httpMethod: 'POST',
            url,
            body: Buffer.from(JSON.stringify(payload)).toString('base64'),
            headers: {
              'Content-Type': 'application/json',
            },
          },
          scheduleTime: {
            seconds: updateAtSeconds
          }
        };

        const [response] = await tasksClient.createTask({ parent: queuePath, task });

        console.log('Created task ${response.name}');
      });



    export const firestoreTtlCallback =
      functions.https.onRequest(async (req, res) => {
        const payload = req.body as UpdateTaskPayload
        try {
          await admin.firestore().doc(payload.docPath).update({ 'prayers.${userId}': false });
          res.send(200);
        }
        catch (error) {
          console.error(error);
          res.status(500).send(error);
        }
      });

谢谢,

4

0 回答 0