Keystone: Postgresql error with Knex Adapter in KeystoneJS 5: 'Error: Cannot read property 'find' of undefined'

Created on 31 Mar 2020  Â·  12Comments  Â·  Source: keystonejs/keystone

I am new to Postgresql (more familiar with Mongo) but our DevOps want me to transition a KeystoneJS 5 project from Mongo to Postgresql. I followed the KeystoneJS instructions and all went fine until I tried adding the 'atCreated' plugin, specifically the 'createdAtField' plugin, to get a created date in my list. I started getting DB errors, and, thinking that I needed to rebuild the database, I ran the KeystoneJS init script to drop the database and rebuild it. Since then, I've gotten the following error:

      $ tsc && cross-env NODE_ENV=development DISABLE_LOGGING=true keystone --entry=tsout/index.js dev --port=4545
        ℹ Command: keystone dev --entry=tsout/index.js --port=4545
        ✔ Validated project entry file ./tsout/index.js
        ✔ Keystone server listening on port 4545
        ✔ Initialised Keystone instance
        â ‹ Connecting to databaseType "OEmbed" is missing a "__resolveType" resolver. Pass false into "resolverValidationOptions.requireResolversForResolveType" to disable this warning.
        setting up cron job
        â ™ Connecting to database  knex:query select 1+1 as result undefined +0ms
        â ¹ Connecting to database  knex:query SHOW server_version; undefined +65ms
        ✖ Connecting to database
        Error: Cannot read property 'find' of undefined
            at /Users/ranthony/Documents/projects/yaa-keystone/node_modules/@keystonejs/utils/dist/utils.cjs.dev.js:57:21
            at async executeDefaultServer (/Users/ranthony/Documents/projects/yaa-keystone/node_modules/@keystonejs/keystone/bin/utils.js:114:3) {
          errors: {
            KnexAdapter: TypeError: Cannot read property 'find' of undefined
                at /Users/ranthony/Documents/projects/yaa-keystone/node_modules/@keystonejs/adapter-knex/lib/adapter-knex.js:258:31
                at Array.forEach (<anonymous>)
                at KnexListAdapter._postConnect (/Users/ranthony/Documents/projects/yaa-keystone/node_modules/@keystonejs/adapter-knex/lib/adapter-knex.js:257:24)
                at /Users/ranthony/Documents/projects/yaa-keystone/node_modules/@keystonejs/adapter-knex/lib/adapter-knex.js:73:19
                at Array.forEach (<anonymous>)
                at KnexAdapter.postConnect (/Users/ranthony/Documents/projects/yaa-keystone/node_modules/@keystonejs/adapter-knex/lib/adapter-knex.js:72:38)
                at KnexAdapter.connect (/Users/ranthony/Documents/projects/yaa-keystone/node_modules/@keystonejs/adapter-knex/node_modules/@keystonejs/keystone/lib/adapters/index.js:29:38)
                at async pReflect (/Users/ranthony/Documents/projects/yaa-keystone/node_modules/p-reflect/index.js:5:17)
                at async Promise.all (index 0)
                at async executeDefaultServer (/Users/ranthony/Documents/projects/yaa-keystone/node_modules/@keystonejs/keystone/bin/utils.js:114:3)
          }
        }

The DB is obviously empty, but I am trying to set up a new database based on the current lists using

    const keystone = new Keystone({
        name: PROJECT_NAME,
        adapter: new KnexAdapter({ dropDatabase: true })
    });

I went back to a prior branch, hoping to undo the errors (except the dropped DB of course), but the error persists. Any help much appreciated, I'm at a bit of a loss what to look for here.

My current Keystone versions are:

    "@keystonejs/adapter-knex": "^7.0.0",
  "@keystonejs/adapter-mongoose": "^5.2.0",
    "@keystonejs/app-admin-ui": "^5.8.0",
    "@keystonejs/app-graphql": "^5.1.0",
    "@keystonejs/app-next": "^5.1.0",
    "@keystonejs/app-static": "^5.1.0",
    "@keystonejs/auth-password": "^5.1.0",
    "@keystonejs/field-content": "^5.4.0",
    "@keystonejs/fields": "^6.3.0",
    "@keystonejs/fields-markdown": "^5.1.0",
    "@keystonejs/fields-wysiwyg-tinymce": "^5.2.0",
    "@keystonejs/file-adapters": "^5.5.0",
    "@keystonejs/keystone": "^5.5.0",
    "@keystonejs/list-plugins": "^5.1.2",
    "@keystonejs/oembed-adapters": "^5.1.0",

All 12 comments

I tried bumping the Knex adapter and keystone versions to both be '7.0.0' following https://github.com/keystonejs/keystone/issues/2588 -- and yet now, even after bumping everything back, I get

$ tsc && cross-env NODE_ENV=development DEBUG=knex:query DISABLE_LOGGING=true keystone  --entry=tsout/index.js dev --port=4545
ℹ Command: keystone dev --entry=tsout/index.js --port=4545
✔ Validated project entry file ./tsout/index.js
✔ Keystone server listening on port 4545
✔ Initialised Keystone instance
✖ Connecting to database
TypeError: commonSessionMiddleware is not a function
    at Keystone.prepare (/Users/ranthony/Documents/projects/yaa-keystone/node_modules/@keystonejs/keystone/lib/Keystone/index.js:737:7)
    at executeDefaultServer (/Users/ranthony/Documents/projects/yaa-keystone/node_modules/@keystonejs/keystone/bin/utils.js:112:42)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Any help much appreciated.

commonSessionMiddleware was removed in 61a70503f6c184a8f0f5440466399f12e6d7fa41. What version of @keystonejs/session is in your dep tree?

EDIT: actually, if keystone is already at 7.0.0, you should already have the right dep 🤔

I set keystone back to "@keystonejs/keystone": "^5.5.0", as I was getting errors last week using the latest packages and (from what I gathered in the issue raised https://github.com/keystonejs/keystone/issues/2582) there were still some issues to work out, so it was suggested I roll back to 5.5.0. For what it's worth keystonejs/session is : @keystonejs/[email protected]

I solved the issue for now by going to my last commit to my remote repo, cloning it to another directory, and now things seem to be OK. This would still be using 5.5.0, etc.

Ah, I misread. I thought you were saying you were having the problem with 7.0.0. If you did a package update and the rolled back one package, you’d need to run yarn again to ensure the older version of the session package was reinstalled.

Thanks -- I did update to 7.0.0 but got a schema-related error (I can't remember now) so _tried_ to rollback, including copying a saved yarn.lock and replacing the package.json, then doing yarn, but I kept getting the commonSessionMiddleware error. I could not solve it, even when I felt I'd rolled everything back. Only cloning a new copy worked. I'd be keen to know what went wrong, because I still need to start where I left off, which is adding the atCreated plugin. Do I need to drop the Postgresql database to do that? I mean, as far as I know, Keystone won't add the necessary tables/columns, right?

Also facing this same issue and stack trace on 7.0.0 on a new project. One created last week does not have the issue.
@ra-external let me know if you would like a working yarn.lock file for a knex postgres backend I have from a project I started last week.

@DJonesCadient -- I would. As said I luckily had a version from earlier I was able to clone and got it working, but just in case.

Which of the two stack traces is the one you're getting? The one cannot read property 'find' of undefined or the commonSessionMiddleware error?

@DJonesCadient I would also. I'm facing the same issue today.

@DJonesCadient I would also. I'm facing the same issue today.

@ejferg Here you go
yarn.lock.txt

@DJonesCadient -- I would. As said I luckily had a version from earlier I was able to clone and got it working, but just in case.

Which of the two stack traces is the one you're getting? The one cannot read property 'find' of undefined or the commonSessionMiddleware error?

@ra-external I am getting the cannot read property 'find' of undefined message

@DJonesCadient you'll need to make sure all your dependencies are updated to latest. especially @keystonejs/keystone and @keystonejs/adapter-knex to 7.0.0 and @keystonejs/fields to 7.0.2.

I believe this issue is solved with the latest release of create-keystone-app (3.0.0). Please re-open with further details if this is still an issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amorote picture amorote  Â·  21Comments

arnaud-zg picture arnaud-zg  Â·  18Comments

thekevinbrown picture thekevinbrown  Â·  31Comments

bpavot picture bpavot  Â·  11Comments

justinmoon picture justinmoon  Â·  13Comments