Home > Back-end >  what is the best way to get data inside a cloud-function in case you don't want to send a respo
what is the best way to get data inside a cloud-function in case you don't want to send a respo

Time:05-09

I Have the following function and I want to do a conditional inside of the snapshot and then do some actions, the current issue is I a can see the first console.log in the logs but the the function is not proceeding in to the snapshot for some reason what is the best way to get data inside a cloud-function ? in case you don't want to send them as response ?

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) => {});
});

CodePudding user response:

If you do not want to return anything from the function, you can simply return null. Also you should log any errors in the catch block so you'll know if something is wrong. Try refactoring the code using async-await syntax as shown below:

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;
});
  • Related