Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
amcc opened this issue Dec 14, 2020 · 5 comments
Open

Comments

@amcc
Copy link

amcc commented Dec 14, 2020

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

amcc commented Dec 15, 2020

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.

@Chuloo
Copy link
Collaborator

Chuloo commented Jan 4, 2021

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 Chuloo added the enhancement New feature or request label Jan 4, 2021
@amcc
Copy link
Author

amcc commented Jan 26, 2021

Totally understand. Same deal here re-holidays :)

will do a PR asap

@amcc
Copy link
Author

amcc commented Dec 1, 2021

This took a few months to happen - sorry people, hope this helps: #140

@cloudinary-raae cloudinary-raae added bug Something isn't working invalid This doesn't seem right and removed enhancement New feature or request labels Jul 27, 2022
@raae
Copy link
Collaborator

raae commented Jul 27, 2022

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

@cloudinary-raae cloudinary-raae added needs more info and removed invalid This doesn't seem right bug Something isn't working labels Jul 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants