Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading