简体   繁体   中英

Firebase 9 cloud function not returning data to reactjs app

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!

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;
    // 👇
    return admin.auth()
        .generatePasswordResetLink(auth)
        .then((link) => {
            return {
                text: `Hello ${dest}`,
                link: link.toString(),
            };
        });
});

Or alternatively you can get rid of the then :

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

    const link = await admin.auth().generatePasswordResetLink(auth);
    return {
        text: `Hello ${dest}`,
        link: link.toString(),
    };
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM