我在this guide之后使用Firebase Cloud Functions部署了一些环境变量。
我正在使用React Native Firebase,并希望访问环境变量。在Firebase文档中,它说您可以通过以下方式访问变量:
const functions = require('firebase-functions');
const request = require('request-promise');
exports.userCreated = functions.database.ref('/users/{id}').onWrite(event => {
let email = event.data.child('email').val();
return request({
url: 'https://someservice.com/api/some/call',
headers: {
'X-Client-ID': functions.config().someservice.id,
'Authorization': `Bearer ${functions.config().someservice.key}`
},
body: {email: email}
});
});
在React Native Firebase docs中,它表示导入和使用Firebase函数,如下所示:
import functions from '@react-native-firebase/functions';
function App() {
const [loading, setLoading] = useState(true);
const [products, setProducts] = useState([]);
useEffect(() => {
functions()
.httpsCallable('listProducts')()
.then(response => {
setProducts(response.data);
setLoading(false);
});
}, []);
注意导入上的差异。
我的代码如下:
import functions from '@react-native-firebase/functions';
const id = functions.config().xyz.id;
const key = functions.config().xyz.key;
我正在抛出一个错误,它是:
TypeError: _functions.default.config is not a function
所有软件包似乎都已正确安装-我的版本如下:
"@react-native-firebase/app": "^6.3.0",
"@react-native-firebase/auth": "^6.3.0",
"@react-native-firebase/crashlytics": "^6.3.0",
"@react-native-firebase/firestore": "^6.3.0",
"@react-native-firebase/functions": "^7.1.0",
"@react-native-firebase/messaging": "^6.3.0",
我哪里出错了,如何将Firebase Cloud Functions与React Native Firebase结合使用以获得已部署的环境变量?
提前感谢。
我已将所有RNFB软件包更新到7.1.0版,但仍然存在相同的问题。
如doc中所述,@react-native-firebase/functions
模块“提供了直接触发已部署的HTTPS Callable functions的功能”。
换句话说,此模块用于从您的前端调用某些Cloud Functions 其中,您可以通过functions.config()
检索一些环境变量。
您无法在Firebase / Google Cloud基础架构的后端执行的Cloud Function代码中,在前端执行的React代码中使用functions.config()
。
只能在Cloud Function中进行检索某些Cloud Function环境配置,在后端。
在可调用的Cloud Function中,您可以做的是返回前端(调用者或使用者)在Cloud Function中检索到的环境变量。
以下内容:
exports.getEnvVariable = functions.https.onCall((data, context) => {
const serviceId = functions.config().someservice.id;
return { serviceId };
});
import functions from '@react-native-firebase/functions';
function App() {
const [loading, setLoading] = useState(true);
const [products, setProducts] = useState([]);
useEffect(() => {
functions()
.httpsCallable('getEnvVariable')()
.then(response => {
const serviceId = response.data.serviceId;
});
}, []);