【发布时间】: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.com 和 from: 'Test Email <MyEmail@gmail.com>' 实际上使用了有效的 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;
【问题讨论】:
标签: firebase react-native react-native-firebase