【问题标题】:Firebase cloud function call client side scriptFirebase 云函数调用客户端脚本
【发布时间】:2020-10-18 23:49:03
【问题描述】:

我在 Reactjs 中有一个脚本,它从 api 获取数据(数字),并在用户打开页面时将这些数字与 Firebase 集合中的数字相加,并且用户可以看到这些数字。 应用程序中会有很多用户,每个用户都会有来自同一个脚本的不同数字

我想知道 Firebase Cloud Functions 是否可以在服务器上运行此客户端脚本并在服务器上执行此号码的计算并将此号码存储在 Firestore 集合中。

我是 nodejs 和云功能的初学者,我不知道这是否可行

从 Api 获取数字

  getLatestNum = (sym) => {
    return API.getMarketBatch(sym).then((data) => {
      return data;
    });
  };

我正在尝试的云功能

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
exports.resetAppointmentTimes = functions.pubsub
  .schedule('30 20 * * *')
  .onRun((context) => {
    const appointmentTimesCollectionRef = db.collection('data');
    return appointmentTimesCollectionRef
      .get() 
      .then((querySnapshot) => {
        if (querySnapshot.empty) {
          return null;
        } else {
          let batch = db.batch();
          querySnapshot.forEach((doc) => {
            console.log(doc);
          });
          return batch.commit();
        }
      })
      .catch((error) => {
        console.log(error);
        return null;
      });
  });

【问题讨论】:

  • 我们需要更多关于getMarketBatch API 的详细信息。你怎么称呼它?对 REST API HTTP 端点的简单调用?
  • 是的,这是对 Rest API 的简单调用,我得到了响应

标签: javascript node.js reactjs firebase google-cloud-functions


【解决方案1】:

确实可以从云函数调用 REST API。您需要使用返回 Promises 的 Node.js 库,例如 axios

您的问题不是 100% 清楚,您要写入哪个特定的 Firestore 文档,但我假设它将在批量写入中完成。

因此,以下几行应该可以解决问题:

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

admin.initializeApp();
const db = admin.firestore();

exports.resetAppointmentTimes = functions.pubsub
.schedule('30 20 * * *')
.onRun((context) => {
    
    let apiData;
    return axios.get('https://yourapiuri...')
        .then(response => {
            apiData = response.data;  //For example, it depends on what the API returns
            const appointmentTimesCollectionRef = db.collection('data');
            return appointmentTimesCollectionRef.get();           
        })
        .then((querySnapshot) => {
            if (querySnapshot.empty) {
                return null;
            } else {
                let batch = db.batch();
                querySnapshot.forEach((doc) => {
                    batch.update(doc.ref, { fieldApiData: apiData});
                });
                return batch.commit();
            }
        })
        .catch((error) => {
            console.log(error);
            return null;
        });
});

需要注意的两点:

  1. 如果您想将 API 结果添加到某些字段值,您需要详细说明您的确切需求
  2. 重要提示:您需要使用“Blaze”定价计划。事实上,免费的“Spark”计划“允许仅向 Google 拥有的服务发出出站网络请求”。请参阅https://firebase.google.com/pricing/(将鼠标悬停在“云功能”标题后面的问号上)

【讨论】:

  • 感谢您提供的信息。是否可以像 axios.get('/index.js') 这样调用我的 React 脚本?api 调用只是我得到的整个脚本的一小部分,在问题中发布它太大了
  • 抱歉,我不明白您的最后评论。您能否通过详细的解释来调整您的问题,也许可以使用流程图。
  • 我想从 firebase 托管中调用我的 React 脚本名称 index.js。我可以在云功能中做到这一点吗?抱歉所有这些问题
  • 对不起,我不明白。什么是 index.js(React 文件或 Cloud Function 文件)?它位于哪里? “调用 index.js FROM firebase 托管”到底是什么意思??
  • index.js 是 React 文件。位置是在 firebase 部署后我的文件所在的位置(对不起,我不知道路径)。 “从 firebase 托管中调用 index.js” => firebase 托管在我在终端中进行 firebase 部署后我的文件所在的位置。我希望这会有所帮助
猜你喜欢
  • 1970-01-01
  • 2018-02-02
  • 1970-01-01
  • 2020-01-28
  • 2019-01-18
  • 2018-09-06
  • 2016-06-27
  • 1970-01-01
  • 2018-06-14
相关资源
最近更新 更多