如何解决在 React Native 中从 android 模拟器连接到在模拟器上运行的云功能
我正在构建一个应用程序并在后端使用 firebase。我使用 react-native-firebase 在我的应用程序中使用 firebase。我开发了登录云功能
exports.login = functions.https.onCall((data,context) => {
console.log("data",data);
console.log("context",context);
return "Login successful";
});
在函数文件夹中运行 npm run serve 时,我会在控制台中获取这些配置。 Console output after running npm run serve inside functions folder for firebase
此外,我还添加了以下代码,以便从运行该应用程序的 android 模拟器连接到云功能。
import functions from "@react-native-firebase/functions"
const [email,setEmail] = useState("");
const [password,setPassword] = useState("");
const handleLogin = () => {
// console.log(email,password);
functions()
.useFunctionsEmulator("URL")
.httpsCallable("login")({ email,password })
.then((response) => {
alert(response);
});
}
对于 URL,我尝试了“http://localhost:5001/”,因为它是函数模拟器正在侦听的端口 Port on which cloud functions is listening。但是在运行应用程序时,我收到此错误 Error on clicking login button in app console。我尝试搜索错误,但没有出现任何相关内容。任何帮助将不胜感激。
这些是我定义的云函数
exports.helloWorld = functions.https.onRequest((request,response) => {
functions.logger.info("Hello logs!",{ structuredData: true });
response.send("Hello from Firebase!");
});
exports.signup = functions.https.onRequest((request,response) => {
console.log("request",request);
response.send("SignUp successfull");
});
exports.login = functions.https.onCall((data,context);
return "SignIn successfull";
});
解决方法
我终于解决了
const handleLogin = async () => {
functions().useFunctionsEmulator("http://localhost:5001")
const response = await functions().httpsCallable("login")({ email,password });
console.log("response",response);
}
这是从正在运行的 android 模拟器在模拟器中本地成功调用云函数所需的所有代码。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。