Error while running import script from npm: APIError: The collection with slug events can't be found

default discord avatar
mvdve
last month
7

I am trying to run an import script with ts-node from npm:

PAYLOAD_CONFIG_PATH=src/payload.config.ts ts-node ./src/scripts/eventImport.ts

But unfortunately i am getting the following error:

APIError: The collection with slug events can't be found

The script is very basic:



const result = await payload.create({
        collection: "events",
        locale: "nl",
        data: {
            title: "title",
            slug: "slug",
        },
    });

Does somebody has a suggestion on what is going wrong here?

  • default discord avatar
    Marťafiixek
    last month

    If you are running this straight from a Node.js file that isn't in any relation to the Express server itself, then you can't use

    payload.create

    without doing

    payload.init


    What do you want to achieve?



    Payload Local API can be used only inside the Express server (without doing the

    payload.init

    ) (and all the subsequent routes) Payload runs on

  • default discord avatar
    mvdve
    last month

    ah ok, i am trying to run a one time import script to migrate data from the old CMS to payload



    My bad, had to scroll down in the documentation and there it is, a script example...



    For future reference:

    https://payloadcms.com/docs/local-api/overview#example-script-using-local-api
  • default discord avatar
    Marťafiixek
    last month

    Yeah, if you init Payload first you can use it, forgot to mention 😂

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.