我正在嘗試創建一個回傳給定塊的所有子塊的遞回函式。
對于一些背景資訊,我使用 Notion API 從頁面中獲取所有塊。一個塊可以有一個子塊,這些子塊也可以有一個子塊。這就是為什么我需要一個遞回函式來檢查所有塊。
實際上,我已經成功地從 google appscript 制作了一個遞回函式,如下所示
// recursive function to get all nested blocks in a flattened array format
function getSubBlocks(blockId) {
const url = `https://api.notion.com/v1/blocks/${blockId}/children?page_size=100`
let options = {
"async": true,
"crossDomain": true,
"method": "get",
"headers": {
"Authorization": `Bearer ${NOTION_API_KEY}`,
"Notion-Version": "2022-02-22",
"Content-Type": "application/json"
}
};
var response = JSON.parse(UrlFetchApp.fetch(url, options).getContentText());;
console.log(response)
let blocks = response.results
Utilities.sleep(50)
// guard clause
if (blocks.length == 0) {
return
}
blocks = blocks.concat(blocks.map((block) => getSubBlocks(block.id)).flat())
// get rid of undefined
blocks = blocks.filter(block => block != undefined)
return blocks
}
我正在嘗試從 node.js 中創建一個相同的功能,但是我在解開所有塊時遇到了麻煩。下面是遞回函式的node-js版本
import fetch from "node-fetch";
// recursive function to get all nested blocks in a flattened array format
async function getSubBlocks(blockId) {
const url = `https://api.notion.com/v1/blocks/${blockId}/children?page_size=100`;
let options = {
async: true,
crossDomain: true,
method: "get",
headers: {
Authorization: `Bearer ${NOTION_API_KEY}`,
"Notion-Version": "2022-02-22",
"Content-Type": "application/json",
},
};
const response = await fetch(url, options);
const r = await response.json();
let blocks = r.results;
// guard clause ends the function if the array is empty
if (blocks.length == 0) {
return;
}
// for each block objects, check for children blocks in a recursive manner
let newBlocks = await blocks
.map(async (block) => await getSubBlocks(block.id))
.flat();
blocks = blocks.concat(newBlocks);
// get rid of undefined
blocks = blocks.filter((block) => block != undefined);
return await blocks;
}
getSubBlocks(testBlock)
.then((r) => console.log(r))
.catch((error) => console.log(error));
我知道上面的函式是一個異步函式,所以它總是回傳一個承諾。這就是為什么我試圖用 then 子句解開承諾,但我只解開第一個塊,而來自遞回呼叫的其他塊仍然作為承諾呈現。示例輸出如下:
[
{
object: 'block',
id: 'XXXXXXXXXXXX',
created_time: '2022-05-28T05:15:00.000Z',
last_edited_time: '2022-05-28T05:15:00.000Z',
created_by: { object: 'user', id: 'XXXXXX' },
last_edited_by: { object: 'user', id: 'XXXXXX' },
has_children: false,
archived: false,
type: 'paragraph',
paragraph: { rich_text: [Array], color: 'default' }
},
{
object: 'block',
id: 'XXXXXXXXXXXX',
created_time: '2022-05-28T05:15:00.000Z',
last_edited_time: '2022-05-28T05:15:00.000Z',
created_by: { object: 'user', id: 'XXXXXXXXXXXX' },
last_edited_by: { object: 'user', id: 'XXXXXXXXXXXX' },
has_children: false,
archived: false,
type: 'paragraph',
paragraph: { rich_text: [], color: 'default' }
},
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
]
有沒有辦法解決這個問題?
從更廣泛的意義上說,在使用遞回和獲取時是否有任何最佳實踐來檢索所有值?
uj5u.com熱心網友回復:
您的getSubBlocks函式不回傳Promise.
此外,您可能希望將方法從map普通for...of回圈更改為簡化代碼并洗掉一些不必要await的 .
試試這種方法:
import fetch from 'node-fetch';
// recursive function to get all nested blocks in a flattened array format
async function getSubBlocks(blockId) {
const url = `https://api.notion.com/v1/blocks/${blockId}/children?page_size=100`;
let options = {
async: true,
crossDomain: true,
method: 'get',
headers: {
Authorization: `Bearer ${NOTION_API_KEY}`,
'Notion-Version': '2022-02-22',
'Content-Type': 'application/json',
},
};
const response = await fetch(url, options);
const r = await response.json();
let blocks = r.results;
// guard clause ends the function if the array is empty
if (blocks && blocks.length == 0) {
return undefined;
}
// for each block objects, check for children blocks in a recursive manner
for (const block of blocks) {
const subBlocks = await getSubBlocks(block.id)
if (subBlocks) blocks = [...blocks, ...subBlocks]
}
return blocks;
}
const res = getSubBlocks(testBlock)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/482920.html
標籤:javascript 节点.js 谷歌应用脚本 递归 承诺
下一篇:根據子條件過濾嵌套的物件陣列
