簡體   English   中英

從本地反應本地客戶端運行 firebase 雲功能時出現“錯誤:內部”

[英]'Error: INTERNAL' when running firebase cloud functions locally from react native client

我通過了有關以下問題的所有問題/答案:從反應本機客戶端本地調用 firebase 雲函數。 但沒有答案幫助我解決它。

我正在使用 react-native@0.61.5 和 react-native-firebase@5.6.0

這是問題:

After I run ' firebase serve --only functions ', when I try to use functions.useFunctionsEmulator(' http://localhost:5000 ') or functions.useFunctionsEmulator(' http://MY-IP-ADDRESS:5000 ')我收到以下錯誤。

在此處輸入圖像描述

注意:從 firebase 服務器調用時,我的雲 function 工作正常(刪除產生錯誤的行代碼時)。

這是我的代碼:

雲 Function:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

exports.addAdminRole = functions.https.onRequest((data, context) => {  

    return admin.auth().getUserByEmail(data.email).then(user => {
        return admin.auth().setCustomUserClaims(user.uid, {
            admin: true
        });
    }).then(() => {
        return {
            message: 'Admin has been added Successfuly!'
        }
    }).catch(err => {
        return err;
    });
});

反應本機 (UI)

import React from 'react'
import { View, Text, TouchableHighlight } from 'react-native'
import firebase from 'react-native-firebase'

const functions = firebase.functions()

//functions.useFunctionsEmulator('http://localhost:5000')   <-- this line produce 'Error: INTERNAL'
//functions.useFunctionsEmulator('http://MY-IP-ADDRESS:5000') <-- using this line also produced the same error

export default class AddAdmin extends React.Component {

    addAdminRole() {
        const addAdminRole = functions.httpsCallable('addAdminRole')
        addAdminRole({ email: 'admin@test.fr' })
            .then(result => console.log(result))
            .catch(err => console.error(err))
    }

 render() {
        return (
                <View style={{ flex: 1, justifyContent: 'center', alignitems: 'center' }}>
                    <TouchableHighlight onPress={this.addAdminRole}>
                            <Text style={styles.buttonText}>Add Admin Role</Text>
                    </TouchableHighlight>
                </View>
                )
 }
}

您似乎正在遵循一些過時的說明。 根據有關啟動模擬器的 Firebase 文檔,您需要:

 firebase emulators:start --only functions

之后,你可以在你的 React 代碼中使用它:

 firebase.functions().useFunctionsEmulator("http://localhost:5001")

我不完全確定react-native-firebase firebase 是否支持useFunctionsEmulator ,所以如果您使用的是 package,您可能需要查看他們的發行說明。

默認情況下 Firebase 仿真器僅響應來自本地主機的請求,要從其他本地設備進行測試,請嘗試將"host": "0.0.0.0"添加到您的firebase.json文件:

"emulators": {
  "functions": {
     "port": 5001,
     "host": "0.0.0.0"
   },
}

此外,配置文件中的.useFunctionsEmulator()應該有您的本地主機 IP 地址,而不是http://localhost ,例如:

firebase.functions().useFunctionsEmulator("http://10.0.0.170:5001");

您必須刪除“http”,因此它將是:

firebase.functions().useEmulator('localhost', '5001')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM