我通过了有关以下问题的所有问题/答案:从本地客户端响应本地调用Firebase云功能。但是没有答案帮助我解决它。
我正在使用react-native@0.61.5和react-native-firebase@5.6.0
这里是问题:
当我尝试使用 functions.useFunctionsEmulator('http://localhost:5000')时,运行' firebase仅提供功能'或 functions.useFunctionsEmulator('http://MY-IP-ADDRESS:5000'),我收到以下错误消息。
注意:当从Firebase服务器调用时(删除产生错误的行代码时),我的云功能正常运行。
这是我的代码:
云功能:
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>
)
}
}
答案 0 :(得分:0)
您似乎正在遵循一些过时的说明。根据{{3}}上的Firebase文档,您需要:
const uint n = get_global_id(0); float4 triangles{1, 2, 3, 4}; // calculated values for each vertex //(float4 list[size];) from constructor list[n] = triangles;
完成后,您可以在React代码中使用它:
firebase emulators:start --only functions
我不确定firebase.functions().useFunctionsEmulator("http://localhost:5001")
是否支持react-native-firebase
,因此,如果您使用的是该软件包,则可能需要查看其发行说明。
答案 1 :(得分:0)
默认情况下,Firebase 模拟器仅响应来自 localhost 的请求,test from other local devices 尝试将 "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");
答案 2 :(得分:0)
你必须删除“http”,所以它是:
firebase.functions().useEmulator('localhost', '5001')