簡體   English   中英

將數據從 Google Cloud 保存到 Firestore Function

[英]Saving data to Firestore from Google Cloud Function

我有一個 function 我已經部署並計划每天早上運行到我的谷歌雲功能中。 它說它運行成功,我什至看到一些我還沒有取出的控制台日志,但是當我查看我的 firestore 集合時,數據沒有更新。 如果我使用節點在我的 Visually Studio 代碼中本地運行相同的 function,那么它可以完美運行。 只是想弄清楚我在這里缺少什么。 這是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);
                  });
            }
        });
    })

我刪除了帳戶訪問詳細信息,因此這些通用值不是問題,僅供參考。

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

我最終切換到 axios,所以一切都是 promise 和基於異步/等待的,它現在正在工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM