如果您不想发送响应,在云功能中获取数据的最佳方法是什么?

what is the best way to get data inside a cloud-function in case you don't want to send a response?

我有以下功能,我想在快照内部做一个条件,然后做一些动作, 当前的问题是我可以在日志中看到第一个 console.log 但该函数由于某种原因没有进入快照 在云函数中获取数据的最佳方法是什么?如果您不想将它们作为回复发送?

export const deleteClient = functions.https.onCall((data, context) => {
  console.log(context.auth?.uid, data.clientAccountId, ':::::::::::::ID1 + ID2:::::::::::::');
  db.collection('ClientsData')
    .doc(data.clientAccountId)
    .get()
    .then((snapshot) => {
      console.log(context.auth?.uid, data.clientAccountId, ':::::::::::::snapshot.data()?.sharedId:::::::::::::');
      if (context.auth?.uid! === snapshot.data()?.sharedId) {
        admin
          .auth()
          .deleteUser(data.clientAccountId)
          .then(() => {
            console.log('Successfully deleted user');
          })
          .catch((error: any) => {
            console.log('Error deleting user:', error);
          });
        db.collection('ClientsData').doc(data.clientAccountId).delete();
      }
    })
    .catch((err) => {});
});

如果您不想 return 函数中的任何内容,您可以简单地 return null。此外,您应该在 catch 块中记录任何错误,以便知道是否有问题。尝试使用 async-await 语法重构代码,如下所示:

export const deleteClient = functions.https.onCall(async (data, context) => {
  try {
    console.log(context.auth?.uid, data.clientAccountId, ':::::::::::::ID1 + ID2:::::::::::::');
    const snapshot = await db.collection('ClientsData').doc(data.clientAccountId).get()
    console.log(context.auth?.uid, data.clientAccountId, ':::::::::::::snapshot.data()?.sharedId:::::::::::::');
    if (context.auth?.uid! === snapshot.data()?.sharedId) {
      await Promise.all([
        admin.auth().deleteUser(data.clientAccountId),
        db.collection('ClientsData').doc(data.clientAccountId).delete()
      ]);
    }
  } catch (e) {
    console.log(e)
  }
  return null;
});