【问题标题】:Firestore add return of function to databaseFirestore 将函数返回添加到数据库
【发布时间】:2020-10-24 14:28:24
【问题描述】:

我正在尝试使用 firebase 创建一个函数,该函数会根据请求执行一些抓取活动,然后每次将结果记录到一个集合中。我的函数工作并返回我需要的项目数组,但我无法将此数组添加到 firestore 数据库。

我不确定我是否需要订阅响应,或者它是否正在返回其他内容。 云功能:

exports.scraper = functions.https.onRequest( async (request, response) => {
    cors(request, response, async () => {

        const body = (request.body);
        const data = await scrapeteamtags(body.text);
        response.send(data)
    });
    return admin.firestore().collection('games').add({
        teams: data
    })
    
});

添加了在等待上下文中使用的函数:

const scrapeteamtags = (text) => {
    const urls = Array.from( getUrls(text) );
    const requests = urls.map(async url => {

        const res = await fetch(url);
        const html = await res.text();
        const $ = cheerio.load(html);
        
        const getTeamlist = JSON.parse($('body').text())  
        
        var gamelist = {
            games: []
        }
    
        getTeamlist.SSResponse.children.map(function(item) {
            // go into the returned json
            var event = new Object;
            var leagues  = ["Premier League", "Spanish La Liga", "Italian Serie A", 'French Ligue 1', 'German Bundesliga']
            // finds all child items that contain the event tag
            
            if(Object.keys(item).includes('event')) {
                // check that the league is on the list which are of interest
                if(leagues.includes(item.event.typeName)) {
                event.id = item.event.id;
                event.name = item.event.name;
                // add the event name and id to the object then go into next level to get market data
                item.event.children.map(function(item1) {
                    if(Object.keys(item1).includes('market')) {
                        event.marketid = item1.market.id
                        // add the market data id to the object
                        var eventoutcome = []

                        item1.market.children.map(function(item2) {
                            if(Object.keys(item2).includes('outcome')) {
                                eventoutcome.push({"id" : item2.outcome.id,
                                "id": item2.outcome.id,
                                "name": item2.outcome.name,
                                "price": item2.outcome.children[0].price.priceDec})
                                //adds the id, name and price to an array, then add it to the object
                                event.outcome = eventoutcome
                            }
                        })
                    }
                })
                gamelist.games.push(event)
            }
            // push each event as a new object to the array of games
            
            }
        })

        //console.log(gamelist.games)
        return {
            gamelist

        }
    });
    
    return Promise.all(requests);
}

【问题讨论】:

    标签: javascript firebase google-cloud-firestore google-cloud-functions cors


    【解决方案1】:

    HTTP 函数不允许您返回带有要发送的数据的承诺。 (这就是可调用函数的工作原理,但这不适用于此处。)您必须等待数据库写入完成,然后发送响应以终止函数。

    函数的结构应该更像这样:

    exports.scraper = functions.https.onRequest( async (request, response) => {
        cors(request, response, async () => {
            const body = (request.body);
            const data = await scrapeteamtags(body.text);
            await admin.firestore().collection('games').add({
                teams: data
            })
            response.send(data)
        });
        
    });
    

    【讨论】:

      猜你喜欢
      • 2018-08-27
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 2023-01-16
      • 2019-07-06
      • 2019-08-12
      相关资源
      最近更新 更多