Javascript Firebase云函数调用客户端脚本

Javascript Firebase云函数调用客户端脚本,javascript,node.js,reactjs,firebase,google-cloud-functions,Javascript,Node.js,Reactjs,Firebase,Google Cloud Functions,我在Reactjs中有一个脚本,它从api中获取数据(数字),并在用户打开页面时将这些数字与Firebase集合中的数字相加,用户可以看到这些数字。 应用程序中会有很多用户,每个用户都会有来自同一脚本的不同数字 我想知道Firebase云函数是否可以在服务器上运行这个客户端脚本,在服务器上调用这些数字,并将这些数字存储在Firestore集合中 我是nodejs和cloud函数的乞丐,我不知道这是否可行 从Api获取数字 getLatestNum = (sym) => { re

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

我想知道Firebase云函数是否可以在服务器上运行这个客户端脚本,在服务器上调用这些数字,并将这些数字存储在Firestore集合中

我是nodejs和cloud函数的乞丐,我不知道这是否可行

从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;
      });
  });

确实可以从云函数调用RESTAPI。您需要使用Node.js库来返回承诺,如

在您的问题中,您还没有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;
        });
});

有两点需要注意:

  • 如果要将API结果添加到某些字段值中,则需要提供有关确切需要的更多详细信息
  • 重要信息:您需要参与“Blaze”定价计划。事实上,免费的“Spark”计划“只允许对谷歌拥有的服务发出出站网络请求”。请参阅(将鼠标悬停在“云功能”标题后的问号上)

  • 我们需要更多关于什么是
    getMarketBatch
    API的详细信息。你怎么称呼它?一个对REST API HTTP端点的简单调用?是的,这是一个对REST API的简单调用,我得到了一个响应,请您提供信息。有没有可能像axios.get('/index.js')那样调用我的React脚本api调用只是我得到的脚本的一小部分,在问题中发布它太大了对不起,我不理解你最后的评论。你能不能用详细的解释和流程图来修改你的问题。我想从firebase主机调用我的React脚本名称index.js。我可以在云函数中执行此操作。对不起,这么多问题,我不明白。什么是index.js(React文件或云函数文件)?它在哪里?“从firebase主机调用index.js”到底是什么意思?index.js是React文件。位置是firebase部署后我的文件所在的位置(对不起,我不知道路径)。“从firebase hosting调用index.js”=>firebase hosting,在终端中部署firebase后,我的文件将存放在何处。我希望这有帮助