简体   繁体   English

将数据从 Google Cloud 保存到 Firestore Function

[英]Saving data to Firestore from Google Cloud Function

I have a function I'ved deployed and scheduled to run every morning into my Google Cloud Functions.我有一个 function 我已经部署并计划每天早上运行到我的谷歌云功能中。 It says it runs successfuly and I even see some of the console logs I haven't taken out yet, but when I look at my firestore collection the data isn't updated.它说它运行成功,我什至看到一些我还没有取出的控制台日志,但是当我查看我的 firestore 集合时,数据没有更新。 If I run this same function locally in my Visually Studio code using node, then it works perfectly.如果我使用节点在我的 Visually Studio 代码中本地运行相同的 function,那么它可以完美运行。 Just trying to figure out what I'm missing here.只是想弄清楚我在这里缺少什么。 Here is the function, it's not very long.这是function,不是很长。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const request = require('request');
const cheerio = require('cheerio');
const cheerioTableparser = require('cheerio-tableparser');
const requestP = require('request-promise');

exports.scrape = functions.pubsub.schedule('0 4 * * *')
    .onRun(async (context) => {

        //Service Account for access
        var serviceAccount = {
            "type": "service_account",
            "project_id": "PROJECT-ID",
            "private_key_id": "PRIVATE-KEY-ID",
            "private_key": "PRIVATE KEY",
            "client_email": "hoopfire-admin@hoopfire-api.iam.gserviceaccount.com",
            "client_id": "CLIENT-ID",
            "auth_uri": "https://accounts.google.com/o/oauth2/auth",
            "token_uri": "https://accounts.google.com/o/oauth2/token",
            "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
            "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/hoopfire-admin%40hoopfire-api.iam.gserviceaccount.com"
        }

        //Init app
        // admin.initializeApp({
        //     credential: admin.credential.cert(serviceAccount)
        // });
        if (admin.apps.length === 0) {
            admin.initializeApp();
        }

        var db = admin.firestore();

        //-------------------------------------------------------- NBA ------------------------------------------------------

        //Make a request to KenPom url for HMTL
        request('https://www.basketball-reference.com/leagues/NBA_2023.html#advanced-team', async function (error, response, html) {
            if (!error && response.statusCode == 200) {
                //Load HTML into Cheerio
                var $ = cheerio.load(html);

                //Load cheerio htl, into cheerioTableParser plugin
                cheerioTableparser($);

                var jsonData = [];
                var data = $("#advanced-team").parsetable(true, true, true);

                //Loop through matrix and created array of objects with associated properties
                for (var i = 1; i < data[0].length; i++) {
                    var team = {
                        "rank": data[0][i],
                        "team": data[1][i],
                        "wins": data[3][i],
                        "losses": data[4][i],
                        "oRtg": data[10][i],
                        "dRtg": data[11][i],
                        "pace": data[13][i]
                    }
                    jsonData.push(team);
                    console.log(team.team);
                }
                //Remove initial elment bc its filled with the titles of the columns
                jsonData.splice(0, 1);

                var _datarwt = [];
                // //Loop through cleaned data and add to the FireStore
                for (var i = 0; i < jsonData.length; i++) {
                    // var ref = db.collection('nba-teams').doc(jsonData[i].team);
                    // ref.set(jsonData[i]);
                    _datarwt.push(db.collection('nba-teams').doc(jsonData[i].team).set(jsonData[i]));
                }

                const _dataloaded = await Promise.all(_datarwt)
                  .then(() => {
                    response.send('NBA COMPLETE');
                  })
                  .catch((err) => {
                    console.log(err);
                  });
            }
        });
    })

I've removed the account access details, so those generic values aren't the issue, just FYI.我删除了帐户访问详细信息,因此这些通用值不是问题,仅供参考。

try {
            const res = await axios.get('https://www.basketball-reference.com/leagues/NBA_2023.html#advanced-team', config)

            //Load HTML into Cheerio
            var $ = cheerio.load(res.data);

            //Load cheerio htl, into cheerioTableParser plugin
            cheerioTableparser($);

            var jsonData = [];
            var data = $("#advanced-team").parsetable(true, true, true);

            //Loop through matrix and created array of objects with associated properties
            for (var i = 1; i < data[0].length; i++) {
                var team = {
                    "rank": data[0][i],
                    "team": data[1][i],
                    "wins": data[3][i],
                    "losses": data[4][i],
                    "oRtg": data[10][i],
                    "dRtg": data[11][i],
                    "pace": data[13][i]
                }
                jsonData.push(team);
                // console.log(team.team);
            }
            //Remove initial elment bc its filled with the titles of the columns
            jsonData.splice(0, 1);

            const _datarwt = [];
            // //Loop through cleaned data and add to the FireStore
            for (var i = 0; i < jsonData.length; i++) {
                _datarwt.push(db.collection('nba-teams').doc(jsonData[i].team).set(jsonData[i]));
            }

            const _dataloaded = await Promise.all(_datarwt)
                .then(() => {
                    console.log('NBA COMPLETE')
                })
                .catch((err) => {
                    console.log(err);
                });
        } catch (err) {
            // Handle Error Here
            console.error(err);
        }

I ended up switching to axios so everything is promise and async/await based and its working now.我最终切换到 axios,所以一切都是 promise 和基于异步/等待的,它现在正在工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM