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

[gatsby-source-drupal] Cannot read property 'map' of undefined #22724

Closed
robdecker opened this issue Apr 1, 2020 · 6 comments · Fixed by #22898
Closed

[gatsby-source-drupal] Cannot read property 'map' of undefined #22724

robdecker opened this issue Apr 1, 2020 · 6 comments · Fixed by #22898
Labels
type: bug An issue or pull request relating to a bug in Gatsby

Comments

@robdecker
Copy link

Description

I'm using the Gatsby Cloud live preview. The build compiles fine, and the preview functionality works. When creating a new Drupal node and Gatsby Cloud attempts to rebuild, it fails/crashes. A manual restart of the live preview works.

This is a blocker since I can't expect the client to restart the live preview after each new Drupal node.

Expected result

The build should complete, and the new Drupal node should be preview-able.

Actual result

10:33:13 AM: TypeError: Cannot read property 'map' of undefined 
 - utils.js:148 handleWebhookUpdate [www]/[gatsby-source-drupal]/utils.js:148:70 
 - gatsby-node.js:217 [www]/[gatsby-source-drupal]/gatsby-node.js:217:20 
 - layer.js:95 Layer.handle [as handle_request] [www]/[express]/lib/router/layer.js:95:5 
 - index.js:317 trim_prefix [www]/[express]/lib/router/index.js:317:13 
 - index.js:284 [www]/[express]/lib/router/index.js:284:7 
 - index.js:335 Function.process_params [www]/[express]/lib/router/index.js:335:12 
 - index.js:275 next [www]/[express]/lib/router/index.js:275:10 
 - read.js:130 [www]/[body-parser]/lib/read.js:130:5
 - index.js:224 invokeCallback [www]/[raw-body]/index.js:224:16 
 - index.js:213 done [www]/[raw-body]/index.js:213:7 
 - index.js:273 IncomingMessage.onEnd [www]/[raw-body]/index.js:273:7
 - task_queues.js:84 processTicksAndRejections internal/process/task_queues.js:84:21

Environment

I only see this on live preview, and not on my local.

@robdecker robdecker added the type: bug An issue or pull request relating to a bug in Gatsby label Apr 1, 2020
@pieh
Copy link
Contributor

pieh commented Apr 2, 2020

@smthomas want to take a look at this?

If @robdecker used one of recent versions the error would happen in here:

https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-drupal/src/utils.js#L150-L152 (converting dist line from error to src line)

And it might happen that referencedNodesLookup.get(newNode) is undefined if the new node didn't have any references, so probably doing sanity check before mapping would be good idea

@smthomas
Copy link
Contributor

smthomas commented Apr 6, 2020

I am able to replicate this issue and am currently working on a fix.

@robdecker
Copy link
Author

Our team has worked on this, and have created a couple patches that seem to be working well.

diff --git a/node_modules/gatsby-source-drupal/utils.js b/node_modules/gatsby-source-drupal/utils.js
index 85c6b8f..ec4cd12 100644
--- a/node_modules/gatsby-source-drupal/utils.js
+++ b/node_modules/gatsby-source-drupal/utils.js
@@ -145,8 +145,11 @@ const handleWebhookUpdate = async ({
     nodesToUpdate.push(...addedReferencedNodes);
   } else {
     // if we are inserting new node, we need to update all referenced nodes
-    const newNodeReferencedNodes = referencedNodesLookup.get(newNode).map(id => getNode(id));
-    nodesToUpdate.push(...newNodeReferencedNodes);
+    const newNodes = referencedNodesLookup.get(newNode);
+    if (typeof newNodes !== `undefined`) {
+      const newNodeReferencedNodes = newNodes.map(id => getNode(id));
+      nodesToUpdate.push(...newNodeReferencedNodes);
+    }
   } // download file
diff --git a/node_modules/gatsby/dist/bootstrap/page-hot-reloader.js b/node_modules/gatsby/dist/bootstrap/page-hot-reloader.js
index 149d82a..a71aea5 100644
--- a/node_modules/gatsby/dist/bootstrap/page-hot-reloader.js
+++ b/node_modules/gatsby/dist/bootstrap/page-hot-reloader.js
@@ -53,7 +53,9 @@ module.exports = graphqlRunner => {
     }
   });
   emitter.on(`DELETE_NODE`, action => {
-    if (action.payload.internal.type !== `SitePage`) {
+    // Gatsby Drupal preview does not send a valid payload.
+    // We need to make sure it is set.
+    if (typeof action.payload !== `undefined` && action.payload.internal.type !== `SitePage`) {
       pagesDirty = true; // Make a fake API call to trigger `API_RUNNING_QUEUE_EMPTY` being called.
       // We don't want to call runCreatePages here as there might be work in
       // progress. So this is a safe way to make sure runCreatePages gets called

cc: @arshad

@shadcn
Copy link
Contributor

shadcn commented Apr 7, 2020

diff --git a/node_modules/gatsby/dist/bootstrap/page-hot-reloader.js b/node_modules/gatsby/dist/bootstrap/page-hot-reloader.js
index 149d82a..a71aea5 100644
--- a/node_modules/gatsby/dist/bootstrap/page-hot-reloader.js
+++ b/node_modules/gatsby/dist/bootstrap/page-hot-reloader.js
@@ -53,7 +53,9 @@ module.exports = graphqlRunner => {
}
});
emitter.on(DELETE_NODE, action => {

  • if (action.payload.internal.type !== SitePage) {
  • // Gatsby Drupal preview does not send a valid payload.
  • // We need to make sure it is set.
  • if (typeof action.payload !== undefined && action.payload.internal.type !== SitePage) {
    pagesDirty = true; // Make a fake API call to trigger API_RUNNING_QUEUE_EMPTY being called.
    // We don't want to call runCreatePages here as there might be work in
    // progress. So this is a safe way to make sure runCreatePages gets called

This one is on NODE DELETE and should most probably happen on the Drupal side.

The Drupal module should be updated to send an empty payload on delete. We'll send a patch on drupal.org.

@smthomas
Copy link
Contributor

smthomas commented Apr 7, 2020

The Delete will need to happen on the Drupal side. I created a stub issue in the Drupal.org issue queue and will follow up on this there. It will require changes to the Drupal module and to the gatsby-source-drupal plugin.

https://www.drupal.org/project/gatsby/issues/3125786

@shadcn
Copy link
Contributor

shadcn commented Apr 9, 2020

Looks good. Thanks @smthomas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants