gatsby-transformer-cloudinary icon indicating copy to clipboard operation
gatsby-transformer-cloudinary copied to clipboard

Cannot convert undefined or null to object - when some image nodes are null

Open amcc opened this issue 4 years ago • 5 comments

The plugin is functioning and we get graphQL nodes created from the Cloudinary images, however we get the following error:

error "gatsby-transformer-cloudinary" threw an error while running the onCreateNode lifecycle:

Cannot convert undefined or null to object

  53 |   const currentNode = basePath === '' ? node : get(node, basePath);
  54 | 
> 55 |   const directAssetDataPaths = Object.keys(currentNode)
     |                                       ^
  56 |     .filter(key => {
  57 |       return currentNode[key] && currentNode[key].cloudinaryAssetData === true;
  58 |     })


  TypeError: Cannot convert undefined or null to object
  
  - Function.keys
  
  - create-asset-nodes-from-data.js:55 getAssetDataPaths
    [mysite]/[gatsby-transformer-cloudinary]/gatsby-node/create-asset-nodes-from-data.js:55:39
...

This occurs in develop and build, in develop it will get past this error, but build fails.

We are adding an object for cloudinary with all the applicable data, but due to our database structure this object is occasionally null. For instance:

mediaItems: [
   {
   ...,
   "cloudinary": {
      "cloudName": "mycloudname",
      "orginalHeight: 2250,
      "cloudinaryAssetData": true,
      ...
       }
   },
   "cloudinary": null
]

Currently the situation is that the plugin appears to be working ok - the above structure is how it looks before the plugin is working. These get replaced with the fixed/fluid image graphQL node and the plugin appears to be working fine. But it still produces the error above, which of course fails the build.

It would be good if this isn't an error! A warning at most, so long as it finds at least some useable objects that would seem to be sufficient. Unfortunately making changes to our DB is a little difficult, there may be a way round this with gatsby-node.js, but it would be good to see if there's a fix perhaps.

amcc avatar Dec 14 '20 21:12 amcc

Further to the above - we've changed the structure on MongoDB so that if there is no image on a mediaItem we put the following:

mediaItems: [
   {
   "cloudinary": {
      "cloudinaryAssetData": false
       }
   }
]

This hasn't solved the error.

I am not sure where issue is coming from - but its certainly something to do with the plugin getting either a null or undefined node somehow. I've tested a fix in create-asset-nodes-from-data.js - which gets rid of the error

gatsby-transformer-cloudinary/gatsby-node/create-asset-nodes-from-data.js from line 52

function getAssetDataPaths({ node, basePath = '' }) {
  // const currentNode = basePath === '' ? node : get(node, basePath);

  // cheeky edit:
  let currentNode = basePath === '' ? node : get(node, basePath);

  if(currentNode === null || currentNode === undefined ){
    currentNode = {}
  }
  
  const directAssetDataPaths = Object.keys(currentNode)

now we have no errors and build works. The plugin is functioning normally - I know the above edit is a hack - can someone perhaps comment and I'm happy to tidy this up with suggestions and do a pull request.

amcc avatar Dec 15 '20 22:12 amcc

Hi, sorry for the delayed response, the holiday happened. Thanks for reporting this use case. Taking a look at this, setting a fallback value for currentNode works, could you send a PR for this?

Chuloo avatar Jan 04 '21 08:01 Chuloo

Totally understand. Same deal here re-holidays :)

will do a PR asap

amcc avatar Jan 26 '21 09:01 amcc

This took a few months to happen - sorry people, hope this helps: https://github.com/cloudinary-devs/gatsby-transformer-cloudinary/pull/140

amcc avatar Dec 01 '21 17:12 amcc

I have a feeling this is invalid from v3.0.0 since cloudinaryAssetData is no longer in use.

raae avatar Jul 27 '22 20:07 raae