TypeError: _functions.default.config is not a function

TypeError: _functions.default.config is not a function

我在 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 软件包更新到 v 7.1.0,但我仍然遇到同样的问题。

doc the @react-native-firebase/functions module "provides the functionality to directly trigger deployed HTTPS Callable functions 中所述。

换句话说,这个模块用于调用,从你的前端,一些在后端执行的云功能,在 Firebase/Google 云基础架构中。

另一方面,检索一些 Cloud Function 环境变量只能在 Cloud Function 中完成,在后端

所以你不能在你的React代码中使用functions.config(),它是在前端执行的。您只能在后端执行的 Cloud Function 代码中执行此操作。


在可调用的 Cloud Function 中,您可以做的是 to return to the front-end(调用者或使用者)在 Cloud Function 中检索的环境变量。

大致如下:

可调用云函数

exports.getEnvVariable = functions.https.onCall((data, context) => {
  const serviceId = functions.config().someservice.id;

  return { serviceId };
});

React 前端

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;
      });
  }, []);