【问题标题】:React Native+Firebase/NodeMailer - Possible Unhandled Promise RejectionReact Native + Firebase / NodeMailer - 可能未处理的承诺拒绝
【发布时间】:2021-08-27 22:31:24
【问题描述】:

我部署了我的 Firebase 代码,该代码使用 this guide 发送测试电子邮件,并且(最终)通过尝试遵循 the React Native Firebase docs 中的示例 sn-p 从我的 React Native 应用程序中成功调用了代码。

但是,当我运行该函数时,React Native 会返回一个Possible Unhanlded Promise Rejection 错误并显示以下日志(显示详细的组件堆栈),并且我没有收到测试电子邮件:

 WARN  Possible Unhandled Promise Rejection (id: 0):
Error: NOT FOUND
Error: NOT FOUND
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.coreApp2:161257:60
invokePassiveEffectCreate@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.coreApp2:19749:32
invokeGuardedCallbackProd@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.coreApp2:6272:21
invokeGuardedCallback@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.coreApp2:6376:42
flushPassiveEffectsImpl@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.coreApp2:19819:36
unstable_runWithPriority@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.coreApp2:54580:30
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.coreApp2:19604:36
workLoop@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.coreApp2:54531:48
flushWork@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.coreApp2:54506:28
_flushCallback@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.coreApp2:54216:24
_callTimer@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.coreApp2:28655:17

以下是 Firebase 控制台中提供的功能日志:

3:56:49.762 PM
sendMail
 got here 

3:56:55.222 PM
sendMail
 {"@type":"type.googleapis.com/google.cloud.audit.AuditLog","status":{},"authenticationInfo":{"principalEmail":"MyEmail@gmail.com"},"serviceName":"cloudfunctions.googleapis.com","methodName":"google.cloud.functions.v1.CloudFunctionsService.UpdateFunction","resourceName":"projects/hb-warranty/locations/us-central1/functions/sendMail"} 

在我的实际日志中,{"principalEmail":"MyEmail@gmail.com"} 显示了我的代码中实际有效的 Gmail 地址。

我觉得我的代码中缺少一些错误处理,我不确定如何使用当前日志来调试我的代码。

应该对我的代码进行哪些更改以改进错误处理,并通过我的 Firebase Cloud Function 成功发送电子邮件?


后端云功能(/MyApp/functions/index.js):

    const functions = require("firebase-functions");
    const admin = require('firebase-admin')
    const nodemailer = require('nodemailer')
    const cors = require('cors')({origin: true});
    admin.initializeApp();
    
    let transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: 'MyEmail@gmail.com',
            pass: 'MyPass'
        }
    });

    console.log('got here');
    // exports.sendMail = functions.https.onRequest((req, res) => {
    // cors(req, res, () => {
    
    exports.sendMail = functions.https.onCall((data) => {
        cors(data, () => {
            const dest = data.dest;
    
            const mailOptions = {
                from: 'Test Email <MyEmail@gmail.com>',
                to: dest,
                subject: 'I am squash',
                html: `<p style="font-size: 16px;">Squash Richard</p>
                    <br />
                    <img src="https://i.etsystatic.com/6129578/r/il/858dbc/703555889/il_570xN.703555889_14z1.jpg" />`
            };
      
            return transporter.sendMail(mailOptions, (erro, info) => {
                if(erro){
                    return erro.toString();
                }
                return 'Sent';
            });
        });    
    });

来自this tutorial 的代码。在我的实际代码中,user: MyEmail@gmail.comfrom: 'Test Email &lt;MyEmail@gmail.com&gt;' 实际上使用了有效的 Gmail 帐户,并且该帐户授权了不太安全的应用程序。


前端 React Native (/MyApp/Email.js):

    import React, {useEffect} from 'react';
    import { View, Text, StyleSheet } from 'react-native';
    import functions from '@react-native-firebase/functions';
    
    function EmailScreen() {
    
    useEffect(() => {
        functions().httpsCallable('sendMail')({dest: 'MyOtherEmail@gmail.com'})
    })
    
        return(
            <View>
                <Text>This is the email screen.</Text>
            </View>
        )
    }
    export default EmailScreen;

来自the docs linked above的代码

【问题讨论】:

    标签: firebase react-native react-native-firebase


    【解决方案1】:

    当 Gmail 服务器无法识别访问帐户的常用 IP 地址时,Gmail 会使用双重身份验证。部署到 Firebase 服务器时,这可能会作为尝试从无法识别的 IP 地址访问您的帐户的问题发生。该代码仍然可以在您的本地计算机上运行。要发送交易电子邮件,您必须使用 sendgrid 或 zoho mails,还有其他几个选项

    【讨论】:

    • 谢谢,但在我删除 cors(data, () =&gt; { ... } 行并使用我的应用重新配置 Firebase SDK 后它起作用了:rnfirebase.io/#3-ios-setup。据我了解,由于我使用的是 NodeMailer,因此不需要 SendGrid 或 Zoho 之类的服务。
    • 你已经部署到云端并且正在发送邮件了吗?
    • 好的,谢谢(需要这些额外的字符来添加评论)
    猜你喜欢
    • 2016-12-15
    • 2017-12-20
    • 1970-01-01
    • 2021-06-25
    • 2016-11-24
    • 2021-11-02
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多