Home > Software engineering >  Firebase 9 cloud function not returning data to reactjs app
Firebase 9 cloud function not returning data to reactjs app

Time:03-10

I apologize if this has been asked before, but everything I find is from 2018/2019 and doesn't seem to help me with my issue. I am using Firebase 9 with a Reactjs app and trying to use the Cloud Functions to retrieve the link from the generatePasswordResetLink in an httpsCallable function. The Cloud Function works fine, logs all the right values, and does return simple strings, like the emails I send in the data value, but for some reason it will not send the link back so I can include it in an email that I will send from the app.

My react function code:

const funcUpdPassword = async () => {
    const sendEmail = user['email'];
    const authEmail = user['username']   '@'   office['domain'];
    const updPassword = httpsCallable(functions, 'updPassword');
    await updPassword({
        dest: sendEmail,
        auth: authEmail,
    }).then((result) => {
        console.log('result data:', result.data);
        const data: any = result.data;
        console.log('data:', data);
    });
};

My firebase function code:

exports.updPassword = functions.https.onCall(async (data, context) => {
    const dest = data.dest;
    const auth = data.auth;
    await admin.auth()
        .generatePasswordResetLink(auth)
        .then((link) => {
            console.log('link:', link.toString());
            console.log(`Hello ${dest}`);

            return {
                text: `Hello ${dest}`,
                link: link.toString(),
            };
        });
});

I have tried it with and without the async/await on both functions and get no errors in the firebase function logs, but still cannot get the link to get sent back. Can someone please tell me why and how to fix this pesky little nuisance? Thanks!

CodePudding user response:

You're not returning anything from the top-level code in your Cloud Function.

In general you should not mix then and await in your code, as it quickly leads to mistakes like this.

One simple fix is to get rid of the await and use return:

exports.updPassword = functions.https.onCall((data, context) => {
    const dest = data.dest;
    const auth = data.auth;
    //            
  • Related