【问题标题】:Using a callable function to send data back to the client from Firebase使用可调用函数将数据从 Firebase 发送回客户端
【发布时间】:2019-07-17 04:10:16
【问题描述】:

我创建了一个可调用的云函数来从 Firebase 读取数据并将结果发送回客户端,但是,只有“null”被返回给客户端。

exports.user_get = functions.https.onCall((data, context) => {

    if (context.auth && data) {
        return admin.firestore().doc("users/" + context.auth.uid).get()

            .then(function (doc) {
                return { doc.data() };
            })

            .catch(function (error) {
                console.log(error);
                return error;
            })
    } return 

});

【问题讨论】:

  • 听起来是时候添加一些调试日志来弄清楚到底发生了什么。
  • @Tor Learner 你知道了吗?
  • 和我一样的问题,但我已经解决了关注这个兄弟,stackoverflow.com/questions/54297558/…

标签: firebase google-cloud-platform google-cloud-functions


【解决方案1】:

我刚刚复制了您从 Cloud Function 连接到 Firestore 数据库并检索数据的案例。正如我所看到的,当您使用"users/" + context.auth.uid 时,您试图以错误的方式访问该字段,该方法找不到该字段,因此它返回一个空值。

我刚刚按照Quickstart using a server client library 文档填充了 Firestore 数据库并使用 node.js 从中获取。

之后我按照Deploying from GCP Console 文档来部署具有以下功能的 HTTP 触发云功能

exports.helloWorld = (req, res) => {  
  firestore.collection('users').get()
  .then((snapshot) => {
    snapshot.forEach((doc) => {
      console.log(doc.id, '=>', doc.data().born);
      let ans = {
        date : doc.data().born
      };
      res.status(200).send(ans);
    });
  })

这是返回所需的字段。

你可以看看我的整个example code here

【讨论】:

  • 我没有问题让它与 Firebase "http" 函数一起使用,问题在于 "Callable" 函数。结果始终为空。
【解决方案2】:

这是因为您正在从数据库 firestore 进行查询,但是云支持团队已经非常酷地保护您的应用程序免受数据泄漏,因此在一个可调用函数中,顾名思义,您只能返回数据您通过 data 参数传递给相同的可调用函数,仅此而已。如果您尝试访问数据库,我建议您使用 onRequest 函数并使用端点获取数据。这样,您不仅可以保护您的数据库,还可以避免数据和内存泄漏。 可以从可调用函数返回的示例

exports.sayHello = functions.https.onCall((data, context) => {
  const name = data.name;
  console.log(`hello ${name}`);
  return `It was really fun working with you ${name}`;
});

首先在您的 index.js 文件中创建一个函数并通过 data 参数接受数据,但正如我所说,您只能返回通过 data 参数传递的数据。

现在调用函数 这是在前端代码中(将事件侦听器附加到按钮或其他东西并触发它

/* jsut say hello from firebase */
callButton.addEventListener('click', () => {
    const sayHello = firebase.functions().httpsCallable('getAllUsers');
    sayHello().then(resutls => {
        console.log("users >>> ", resutls);
    });
});

您可以像这样使用 onRequest 获取数据

    /* get users */
exports.getAllUsers = functions.https.onRequest((request, response) => {
  cors(request, response, () => {
    const data = admin.firestore().collection("users");
    const users = [];
    data.get().then((snapshot) => {
      snapshot.docs.forEach((doc) => {
        users.push(doc.data());
      });
      return response.status(200).send(users);
    });
  });
});

在前端代码中使用 fetch() 来获取新 onRequest 函数的响应,您可以在 Firebase 控制台仪表板中获取函数的端点。

但要从前端代码访问端点,您需要将 cors 添加到您的 firebase 云函数以允许访问端点。 您可以通过将此行添加到 firebase 函数目录的 index.js 文件的顶部来做到这一点

const cors = require("cors")({origin: true});

【讨论】:

    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多