payloadcms / next-payload

A utility to deploy Payload serverlessly within a Next.js app
308 stars 44 forks source link

TypeError: this.config.db is not a function #86

Closed johannesschaffer closed 9 months ago

johannesschaffer commented 10 months ago

Just created a brand new project with Next.js 14. Can't open /admin. The error message

 ⨯ node_modules\payload\dist\payload.js (207:30) @ db
 ⨯ TypeError: this.config.db is not a function
    at BasePayload.init (webpack-internal:///(api)/./node_modules/payload/dist/payload.js:76:31)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async getPayload (webpack-internal:///(api)/./node_modules/payload/dist/payload.js:244:26)
    at async getPayloadClient (webpack-internal:///(api)/./payload/payloadClient.ts:39:25)
    at async eval (webpack-internal:///(api)/./node_modules/@payloadcms/next-payload/dist/middleware/withPayload.js:9:23) {
  page: '/api/[collection]/init'
}
null

The same also goes for the api endpoints like '/api/[collection]/me', ...

johannesschaffer commented 10 months ago

Ok, found the problem myself. The next-payload install command isn't updated for v2. It creates an outdated payload config. I'm gonna leave that issue open for now until it creates a correct payload config,

ChristianMay21 commented 10 months ago

I was able to get things up and running with Next 14 and Payload 2 using the following config, based off of the errors thrown by the default config and some example configs in the documentation:

There were a few modules I had to install: @payloadcms/richtext-slate and @payloadcms/db-mongodb.

I also had to make sure that I had MongoDB installed on my computer (doh). Just throwing that out there because it's not actually stated in the README here that you should have it installed.

import path from 'path';
import { buildConfig } from 'payload/config';
import { BaseDatabaseAdapter } from 'payload/database';
import { Payload } from 'payload/dist/payload';
import { slateEditor } from '@payloadcms/richtext-slate';
import { mongooseAdapter } from '@payloadcms/db-mongodb'

export default buildConfig({
  collections: [
    {
      slug: 'pages',
      fields: [
        {
          name: 'title',
          type: 'text',
          required: true,
        },
        {
          name: 'content',
          type: 'richText',
          required: true,
        },
      ],
    },
  ],
  db: mongooseAdapter({
    url: process.env.MONGODB_URI as string,
  }),
  editor: slateEditor({}),
  globals: [
    // Your globals here
  ],
  typescript: {
    outputFile: path.resolve(__dirname, '../payload-types.ts'),
  }
});
JarrodMFlesch commented 9 months ago

After the next release this will work as you would expect when running yarn next-payload install. Closing, thanks for reporting!