在 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 模拟器在模拟器中本地成功调用云函数所需的所有代码。