首页 > 解决方案 > 从反应原生的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 时,我在控制台中获取了这些配置。 在firebase的functions文件夹中运行npm run serve后的控制台输出

我还添加了以下代码,以从运行应用程序的 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/”,因为它是函数模拟器正在侦听的 端口 云函数正在侦听的端口。但是在运行应用程序时,我收到此错误 Error on click 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) => {
   console.log("data", data);
   console.log("context", context);
   return "SignIn successfull";
});

标签: androidfirebasereact-nativegoogle-cloud-functionsfirebase-tools

解决方案


我终于可以解决了

const handleLogin = async () => {
        functions().useFunctionsEmulator("http://localhost:5001")
        const response = await functions().httpsCallable("login")({ email, password });
        console.log("response", response);
    }

这是从正在运行的 android 模拟器在模拟器中本地成功调用云函数所需的所有代码。


推荐阅读