【问题标题】:Pagination in ZapierZapier中的分页
【发布时间】:2020-01-29 02:55:56
【问题描述】:

我正在尝试使用以下代码从 Zapier 中的分页 API 中获取所有记录。

const limitPerPage = 20;
const apiUrl = "https://myurl.com/data";
var lastCursor = null;
var output = null;

const getContent = async function (cursor) {
    let actualUrl = apiUrl + `?cursor=${cursor}&limit=${limitPerPage}`;
    var apiResults = await fetch(actualUrl)
        .then(resp => {
            return resp.json;
        });
}

const getEntireContentList = async function (cursor) {
    const results = await getContent(cursor);
    console.log("Retreiving data from API for cursor : " + cursor);
    if (results.metadata.cursor !== "") {
        return results.concat(await getEntireContentList(results.metadata.cursor));
    } else {
        return results;
    }
};


(async() => {
    const entireList = await getEntireContentList();
    console.log(entireList);
    output = entireList;
    callback(null, entireList);
})();

我得到错误 您没有定义output!试试output = {id: 1, hello: await Promise.resolve("world")};

我该如何解决这个问题?

【问题讨论】:

    标签: javascript zapier


    【解决方案1】:

    您的问题是,尽管您在该函数中awaiting,但在您的代码有机会运行之前,顶层继续执行并结束。

    好消息是,Zapier 已经将您的代码包装在异步函数中,因此您可以在顶层使用 await(根据 these docs)。

    试试这个:

    const limitPerPage = 20;
    const apiUrl = "https://myurl.com/data";
    let lastCursor = null;
    // var output = null; // zapier does this for you already
    
    const getContent = async function (cursor) {
        const actualUrl = apiUrl + `?cursor=${cursor}&limit=${limitPerPage}`;
        const rawResponse = await fetch(actualUrl)
        return resp.json() // async function, you had it as a property
    }
    
    const getEntireContentList = async function (cursor) {
        const results = await getContent(cursor);
        console.log("Retreiving data from API for cursor : " + cursor);
        if (results.metadata.cursor !== "") {
            return results.concat(await getEntireUserList(results.metadata.cursor)); // should this be named getEntireContentList?
        } else {
            return results;
        }
    };
    
    
    return {
      results: await getEntireContentList()
    }
    

    我注意到这是一种递归方法。这很好,但请记住您的执行时间有限。您还可能会达到内存限制(取决于您要返回的对象数量),因此请密切注意。

    【讨论】:

      猜你喜欢
      • 2020-07-17
      • 2020-04-03
      • 2018-02-28
      • 2017-05-07
      • 1970-01-01
      • 2021-02-11
      • 2020-02-19
      • 2021-06-27
      • 1970-01-01
      相关资源
      最近更新 更多