Jonesus / gatsby-source-directus7

Source plugin for pulling data into GatsbyJS from Directus CMS (https://directus.io)
16 stars 14 forks source link

Cannot read property 'directus' of undefined #11

Closed visualcookie closed 5 years ago

visualcookie commented 5 years ago

Hey.

I have this weird issue, that after a couple of minutes and a fresh yarn start, the plugin is returning errors. I don't know what exactly is causing the issue.

UPDATE: This issue only happens, if there is an image in the content or in the teaser field.

Plugin part from logs:

gatsby-source-directus info Directus Data Fetcher initializing...
gatsby-source-directus info targetStatus is: draft
gatsby-source-directus success Connected to Directus!
gatsby-source-directus success Logged in to Directus!
gatsby-source-directus info Fetching Directus file data...
gatsby-source-directus success Found 1 files from Directus.
gatsby-source-directus info Downloading Directus files to Gatsby build cache...
error (node:75578) [DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated
⠀
gatsby-source-directus success Downloaded all 1 files from Directus!
gatsby-source-directus info Fetching Directus Collection data...
gatsby-source-directus info Fetching Directus Items data...
gatsby-source-directus info Status matched for posts 1. Using item
gatsby-source-directus info Fetching Directus Relations data...
gatsby-source-directus info Mapping Directus relations to Items...
gatsby-source-directus info Cleaning junction collections...
gatsby-source-directus info Mapping Directus files to Items...
gatsby-source-directus info Mapping files for posts...
error Plugin gatsby-source-directus7 returned an error

  TypeError: Cannot read property 'directus' of undefined

  - process.js:300 
    [test-gatsby]/[gatsby-source-directus7]/process.js:300:77

  - Array.map

  - process.js:298 
    [test-gatsby]/[gatsby-source-directus7]/process.js:298:67

  - Array.forEach

  - process.js:296 mapFilesToNodes
    [test-gatsby]/[gatsby-source-directus7]/process.js:296:24

  - gatsby-node.js:75 Object.exports.sourceNodes
    [test-gatsby]/[gatsby-source-directus7]/gatsby-node.js:75:55

  - task_queues.js:89 processTicksAndRejections
    internal/process/task_queues.js:89:5

success source and transform nodes - 2.427 s
success building schema - 0.208 s
error GraphQL query returned error: TypeError: Cannot read property 'allDirectusPost' of undefined
success createPages - 0.018 s
success createPagesStatefully - 0.036 s
success onPreExtractQueries - 0.004 s
success update schema - 0.018 s
error GraphQL Error Encountered 1 error(s):
- Unknown field 'directusPost' on type 'Query'.

      file: /Users/deanhidri/Fintory/test-gatsby/src/templates/post.js

Posts collection:

image

Jonesus commented 5 years ago

Hi!

Do you think you could supply me with a database dump of your current configuration so that I could try and recreate the issue on my end?

visualcookie commented 5 years ago

Hey @Jonesus, sure. I did a dump of the post collection, guess that should be enough, right?

From what I can see now is, that the teaser field in there is NULL. When I go to my post, and check, there is in fact no teaser image, but it was there when I saved and it's in my file library as well. When I select the image on the post I'm editing, I get a Directus error Internal Server Error. Guess it has something to do with Directus then?

Dump => posts.gz

Jonesus commented 5 years ago

Awesome, thanks! Do you think you could also supply the directus_relations table, as there might be some weird relation artifacts going on messing with the proper linking?

visualcookie commented 5 years ago

Hey, I don't have the same setup as yesterday available anymore. I'm just testing the translations right now and have removed the images for now. I may come back to this issue, when #12 has been figured out.

KB1RD commented 5 years ago

I first encountered a similar issue when Directus included old relations that could not be found. This appears to be a bug in Directus that causes old relations to hang around and a bug in this code that attempts to find a table that does not exist. Unfortunately, I do not remember where this was in the code. I'll try to look for it.

The second time I encountered this issue, it was because the image column was NULL. The error is in the following code:

    collectionsWithFiles.forEach(c => {
        info(`Mapping files for ${c.collectionName}...`);
        newEntities[c.collectionName] = newEntities[c.collectionName].map(e => {
            const targetFileId = e[c.fieldName];
            const fileId = files.find(f => f.directus.directusId === targetFileId).directus.id;
            return { ...e, [`${c.fieldName}___NODE`]: fileId };
        });

Notice that the output of files.find(...) is directly accessed right after. The file object should be checked to ensure that it is defined before attempting to access .directus.id. The issue in this case was that null is not an actual file ID, so it wasn't found.

Here is some code that might fix the problem:

    collectionsWithFiles.forEach(c => {
        info(`Mapping files for ${c.collectionName}...`);
        newEntities[c.collectionName] = newEntities[c.collectionName].map(e => {
            const targetFileId = e[c.fieldName];
            const fileObject = files.find(f => f.directus.directusId === targetFileId);
            if (!fileObject || !fileObject.directus || !fileObject.directus.id) {
                warn(
                    `Cannot find file with ID ${targetFileId}. Please check that the field ` +
                        + `${c.fieldName} in the collection ${c.collectionName} is filled out.`
                );
                return e;
            }
            const fileId = fileObject.directus.id;
            return { ...e, [`${c.fieldName}___NODE`]: fileId };
        });

(Standard disclaimer: I haven't tested it yet :P )

Jonesus commented 5 years ago

Thanks @KB1RD ! Now that I've dug a bit more into issues like these I've noticed that some of the object accesses are quite greedy and have probably caused more of similar issues... I'm in progress of writing a test suite for the package (something that should've been done a long time ago...) so that all similar issues would get caught. I'll try your solution out and also try and find similar holes in the code and push out a patch soon-ish!

Jonesus commented 5 years ago

Hey @visualcookie and @KB1RD ! I pushed a new v0.8.0 release which should hopefully address these issues, let me know if they still persist for you after this!

visualcookie commented 5 years ago

Hey @Jonesus. Thanks for the update. Will give it a try with some articles tomorrow and give you feedback on that as soon as I have something ready.

visualcookie commented 5 years ago

Works. 👍