Skip to content
This repository has been archived by the owner on Apr 26, 2022. It is now read-only.

Compile fails #34

Closed
shendriksza opened this issue Aug 22, 2019 · 10 comments
Closed

Compile fails #34

shendriksza opened this issue Aug 22, 2019 · 10 comments

Comments

@shendriksza
Copy link

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
Copy link
Contributor

notrab commented Aug 22, 2019

Please see #23

@notrab notrab closed this as completed Aug 22, 2019
@shendriksza
Copy link
Author

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
Copy link
Author

shendriksza commented Aug 22, 2019

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
Copy link
Contributor

notrab commented Aug 22, 2019

@shendriksza not sure I follow...

You can make the following query:

{
  allMoltinProduct {
    nodes {
      id
      mainImageHref
    }
  }
}

What does that query return inside GraphiQL?

@shendriksza
Copy link
Author

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
Copy link
Author

@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
Copy link
Contributor

ynnoj commented Aug 23, 2019

@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
Copy link

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

@shendriksza
Copy link
Author

@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
Copy link

@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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

4 participants