moltin / gatsby-source-moltin

🚀 Gatsby source plugin for building Elastic Path Commerce Cloud powered eCommerce websites
https://www.gatsbyjs.org/packages/@moltin/gatsby-source-moltin
MIT License
21 stars 4 forks source link

Compile fails #34

Closed shendriksza closed 5 years ago

shendriksza commented 5 years ago

A new error cropped up for me while compiling:

error UNHANDLED REJECTION

  TypeError: Cannot destructure property `link` of 'undefined' or 'null'.

  - gatsby-node.js:37 processProduct
    [kooi-dev-store]/[@moltin]/gatsby-source-moltin/gatsby-node.js:37:25

  - gatsby-node.js:210 products.forEach
    [kooi-dev-store]/[@moltin]/gatsby-source-moltin/gatsby-node.js:210:15

  - Array.forEach

  - gatsby-node.js:208 createProducts
    [kooi-dev-store]/[@moltin]/gatsby-source-moltin/gatsby-node.js:208:14

  - gatsby-node.js:222 Object.exports.sourceNodes
    [kooi-dev-store]/[@moltin]/gatsby-source-moltin/gatsby-node.js:222:9

  - next_tick.js:81 processTicksAndRejections
    internal/process/next_tick.js:81:5

In your gatsby-node.js where you destructure the main-image href:

const {
          link: { href }
        } = main_images.find(
          main_image =>
            main_image.id === product.relationships.main_image.data.id
        )

Any idea why this is happening? Is there an issue with Moltin at the moment, not providing links to my images? Or does this happen because I might have posted images to Moltin without "public: true"?

Either way though, the plugin fails compilation due to it

notrab commented 5 years ago

Please see https://github.com/moltin/gatsby-source-moltin/issues/23

shendriksza commented 5 years ago

Hi thanks for the link.

I verified, but it looks to me like my Moltin ID is still being used where it should be. The same master build that compiled yesterday is also failing now on Netlify with the same error.

I gave all my products main images, now the error states:

error Plugin @moltin/gatsby-source-moltin returned an error

  TypeError: Cannot read property 'href' of undefined

  - gatsby-node.js:272 imageNodes.filter.fileNode
    [kooi-dev-store]/[@moltin]/gatsby-source-moltin/gatsby-node.js:272:55

  - Array.filter

  - gatsby-node.js:271 getFileNodes
    [kooi-dev-store]/[@moltin]/gatsby-source-moltin/gatsby-node.js:271:33

  - gatsby-node.js:299 Object.exports.onCreateNode
    [kooi-dev-store]/[@moltin]/gatsby-source-moltin/gatsby-node.js:299:32

So it still seems some links are missing. Anything else I could possibly check?

shendriksza commented 5 years ago

I̶t̶ ̶s̶e̶e̶m̶s̶ ̶t̶o̶ ̶m̶e̶ ̶t̶h̶e̶ ̶M̶o̶l̶t̶i̶n̶ ̶A̶P̶I̶ ̶c̶h̶a̶n̶g̶e̶d̶?̶ ̶I̶ ̶r̶e̶m̶e̶m̶b̶e̶r̶ ̶i̶t̶ ̶u̶s̶e̶d̶ ̶t̶o̶ ̶r̶e̶t̶u̶r̶n̶ ̶t̶h̶e̶ ̶m̶a̶i̶n̶I̶m̶a̶g̶e̶H̶r̶e̶f̶,̶ ̶n̶o̶w̶ ̶y̶o̶u̶ ̶h̶a̶v̶e̶ ̶t̶o̶ ̶d̶e̶r̶e̶f̶e̶r̶e̶n̶c̶e̶ ̶t̶h̶e̶ ̶m̶a̶i̶n̶I̶m̶a̶g̶e̶ ̶f̶r̶o̶m̶ ̶t̶h̶e̶ ̶r̶e̶l̶a̶t̶i̶o̶n̶s̶h̶i̶p̶ ̶i̶d̶'̶s̶ ̶f̶i̶r̶s̶t̶

notrab commented 5 years ago

@shendriksza not sure I follow...

You can make the following query:

{
  allMoltinProduct {
    nodes {
      id
      mainImageHref
    }
  }
}

What does that query return inside GraphiQL?

shendriksza commented 5 years ago

Sorry I was distracted on a tangent.

I have identified the issue. It seems when we updated/deleted images on Moltin, the references to those images where not deleted as we assumed they were. So there are relationship id's on our products to images that don't exist anymore.

shendriksza commented 5 years ago

@notrab I fixed the issue by writing a script that fetches all products and verifies that each relationship id exists and deleting it when it does not.

Is it really the responsibility on our side, to verify the integrity of Moltin's internal references? Shouldn't the relationships be deleted automatically once the file no longer exists?

ynnoj commented 5 years ago

@shendriksza Thanks for highlighting this issue. This is an issue with the API in which the main_image or file relationship field to the deleted file is not removed from the product record.

I've raised this internally.

domtaylor commented 5 years ago

@shendriksza can you share your solution. I'm running into the same issue. Thanks!

shendriksza commented 5 years ago

@domtaylor basicallly the following (Python code)

The "moltin" in this case is just a small wrapper library I wrote to call the Moltin endpoints

# Check file relationships to make sure they are valid
def checkRelationships():
  moltin.getProducts()

  # Iterate and verify current file ID's are valid
  for product in moltin.allProducts:
    if 'main_image' in product['relationships']:
      fileItem = product['relationships']['main_image']['data']
      # Product has a main image. Let's make sure it exists
      resp = moltin.getFile(fileItem)
      if (not resp):
        moltin.deleteMainFileRelationship(product, fileItem)

    if 'files' in product['relationships']:
      # Product has other images. Let's make sure they exists
      for fileItem in product['relationships']['files']['data']:
        resp = moltin.getFile(fileItem)
        if (not resp):
          moltin.deleteFileRelationship(product, fileItem)
domtaylor commented 5 years ago

@shendriksza thanks so much for sharing. In the end I just created a new store and re-uploaded my products as I only have a few, but in case this happens again I'll rewrite your script for GatsbyJS.