Gatsby: Problem with sourceNodes promises

Created on 24 May 2019  路  1Comment  路  Source: gatsbyjs/gatsby

Hi there, does anyone have any idea why nodes are not being created?

exports.sourceNodes = async ({
  actions,
  createNodeId,
  createContentDigest
}) => {
  const { createNode } = actions
  const apiKey = process.env.PLACES_API_KEY
  const placeIds = listingsJson.map(n => n.places_id).filter(n => n)

  placeIds.map(async placeId => {
    await axios
      .get("https://maps.googleapis.com/maps/api/place/details/json", {
        params: {
          key: apiKey,
          fields: "name,rating,price_level,website,opening_hours",
          placeid: placeId
        }
      })
      .then(response => {
        const node = response.data.result
        console.log(node)
        const jsonNode = JSON.stringify(node)
        return createNode({
          id: createNodeId(`place-${placeId}`),
          parent: null,
          field: jsonNode,
          children: [],
          internal: {
            type: "Places",
            content: jsonNode,
            contentDigest: createContentDigest(node)
          }
        })
      })
  })
}
question or discussion

Most helpful comment

We need to wait for all promises created from placeIds to finish - right now they are not being awaited - please use something like:

-placeIds.map(async placeId => {
+await Promise.all(placeIds.map(async placeId => {

(+ adding closing ) to match one added for Promise.all

>All comments

We need to wait for all promises created from placeIds to finish - right now they are not being awaited - please use something like:

-placeIds.map(async placeId => {
+await Promise.all(placeIds.map(async placeId => {

(+ adding closing ) to match one added for Promise.all

Was this page helpful?
0 / 5 - 0 ratings