How to loop through this data in JavaScript?

I’m never able to remember how this is done

const getPage = async (pageId) => {
  const data = {};
  const response = await notion.pages.retrieve({ page_id: pageId });
  data.id = pageId;
  data.name = response.properties.Name.title[0].plain_text;
  data.imageUrl = response.properties.img.rich_text[0].plain_text;
  data.stock = response.properties.stock.rich_text[0].plain_text;
  data.price = `£${response.properties.price.number}`;
  return data;
};

The above return a data object when I pass it an id. The I want to add each object in an array

const getPosts = async () => {  
  const { results } = await notion.request({
    path: `databases/${process.env.NOTION_DATABASE_ID}/query/`,
    method: "POST",
  });
  const test = results.map((prop) => {
    const id = prop.id;
    const data = [];
    getPage(id).then((t) => data.push(t)); // this is the problem right?
    return data;
  });
  console.log(test);
};

getPosts();

I’m sure I did it before but I have no idea how.

I’ve never been very good with the whole… async-return cycle but I believe it’s something to do with…

Your return isn’t waiting for the getPage to come back?

Yes that’s the problem but how do I fix it.

I’m… going to be guessing here, but…something like

let data = [await getPage(id)];
return data;

?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.