Firebase异步调度程序问题

时间:2020-08-01 13:13:46

标签: javascript firebase async-await google-cloud-functions

我是Firebase和异步javascript的新手。我正在尝试在Firebase中创建一个计划任务,以从一个集合中获取一堆rss URL,将其解析并将其存储在另一个集合中,但是我遇到错误:4 DEADLINE_EXCEEDED:Object.callErrorFromStatus执行时已超过了截止日期。

const refreshRSS = functions.pubsub.schedule('every 30 mins').onRun(async context => {
    let newRSS = addRSS();
    return await newPodcasts;
});

addRSS = async () => {
    const newRSSFeed = new Array();
    let rssURLs = await db.collection('rssURLs').get();
    rssURLs.forEach(async rssURLsObject=>{
        rss = rssURLsObject.data();
        let rssData = await parser.parseURL(rss.url);             
        newRSSFeed.push(db.collection('rss').doc(encodeURIComponent(rss.url))
        .set(podcast));

    })
    return newRSSFeeds;
}

我还尝试在refreshRSS内部返回Promise.all(newRSS),但它引发了另一个错误,指出:不可迭代。 我不确定firebase究竟期望返回参数是什么。

1 个答案:

答案 0 :(得分:0)

当您必须循环解决promise时,请尝试使用map。这将起作用:

const refreshRSS = functions.pubsub.schedule('every 30 mins').onRun(async context => {
    let newRSS = await addRSS();
    return await newPodcasts;
});

addRSS = async () => {
    const newRSSFeed = new Array();
    let rssURLs = await db.collection('rssURLs').get();
    let promise = rssURLs.map(async rssURLsObject=>{
        rss = rssURLsObject.data();
        let rssData = await parser.parseURL(rss.url);  
        let con = await db.collection('rss').doc(encodeURIComponent(rss.url)
        newRSSFeed.push(con)
        .set(podcast));

    })
    await Promise.all(promise)
    return newRSSFeeds;
}