how to import payload from non-root folder

default discord avatar
megetronlast year
11

when init payload from a different non root folder i get errors that collection cannot be found.



creating a mono-repo with payload and set a 'scrapper' folder with package.json file so my directory structure as follows:


.


├── dist


│   ├── blocks


│   ├── collections


│   ├── globals


│   ├── payload-types.js


│   ├── payload.config.js


│   ├── resolvers


│   └── server.js


├── nodemon.json


├── package.json


├── scrappers


│   ├── scrapper.mjs //--> Here i have import payload from 'payload';


│   ├── package.json


├── src


│   ├── blocks


│   ├── collections


│   ├── generated-schema.graphql


│   ├── globals


│   ├── payload-types.ts


│   ├── payload.config.ts


│   ├── resolvers


│   └── server.ts


├── tsconfig.json


└── yarn.lock




my package.json content:



...
    "scrapper:productlist": "PAYLOAD_CONFIG_PATH=../dist/payload.config.js node scrapper.mjs"
...


and i am executing the yarn run scrapper:productlist from within the scrappers directory



what am i missing?

  • default discord avatar
    thisisnotchrislast year

    Good morning!



    For context, what's a scrapper folder?

  • default discord avatar
    megetronlast year

    good morning, here it's already afternoon:)


    please see the 'scrappers' directory above, there in this directory i have a scrapper.mjs file which inits the payload module.

  • discord user avatar
    denolfe
    last year

    Interesting, so looks like the relative path to your config isn't working? Can you show the exact error?



    If you temporarily paste the config into the

    scrapers

    directory and adjust the path, does it work as expected?

  • default discord avatar
    megetronlast year

    also tried with absolute path without success. i am getting the following errors:


    [17:31:23] INFO (payload): Starting Payload...
    (node:39766) UnhandledPromiseRejectionWarning: APIError: The collection with slug facets can't be found.
        at new ExtendableError (/Users/guy/proj/contra/node_modules/payload/dist/errors/APIError.js:22:15)
        at new APIError (/Users/guy/proj/contra/node_modules/payload/dist/errors/APIError.js:38:9)
        at findLocal (/Users/guy/proj/contra/node_modules/payload/dist/collections/operations/local/find.js:16:15)
        at Payload.BasePayload.find (/Users/guy/proj/contra/node_modules/payload/dist/payload.js:53:20)
        at /Users/guy/proj/contra/dist/blocks/filter-by-facets.js:48:60
        at step (/Users/guy/proj/contra/dist/blocks/filter-by-facets.js:33:23)
        at Object.next (/Users/guy/proj/contra/dist/blocks/filter-by-facets.js:14:53)
        at /Users/guy/proj/contra/dist/blocks/filter-by-facets.js:8:71
        at new Promise (<anonymous>)
        at __awaiter (/Users/guy/proj/contra/dist/blocks/filter-by-facets.js:4:12)
    (Use `node --trace-warnings ...` to show where the warning was created)
    (node:39766) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
    (node:39766) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.


    copy the payload.config.js from dist to scrappers folder:

    (node:39433) UnhandledPromiseRejectionWarning: Error: Cannot find module '../resolvers/productBySlug'
  • discord user avatar
    denolfe
    last year

    Do you have a collection with a slug 'facets'? Where are you referencing that from? A relationship maybe?



    It also looks like a

    find

    operation is being attempted. Do you have anything in an

    onInit

    ?

  • default discord avatar
    megetron12 months ago

    great questions, investigating around the facets collection it seems like the afterRead hook imports payload, and shows the error. removing the hook solves the problem.




    here is the facets collection, do i do something wrong?




    import { Block , FieldHook} from 'payload/types';
    import payload from 'payload';
    
    const getFacets: FieldHook = async () => {
      const facets = await payload.find({
        collection: 'facets',
      });
      
      
      if (facets.docs) {
        const ids = facets.docs.map(obj => obj.values.map(val => val.id)).flat();
        return ids
        
      } 
      
      return null;
    };
    
    
    export const FilterFacetsBlock: Block = {
      slug: 'FilterFacets',
      fields: [
        {
          name: 'Facets',      
          type: 'relationship',
          relationTo: 'facets',
          hasMany: true,
          // hooks: {  
          //   afterRead: [getFacets], 
          // },
        },
    
        {
          name: 'ContainsAny',
          type: 'checkbox',
        },
        
      ]
    };
  • discord user avatar
    denolfe
    12 months ago

    Within the field hook, you should use

    req.payload

    .



    Give this a shot and see if that makes a difference



    const getFacets: FieldHook = async ({ req }) => {
      const facets = await req.payload.find({
        collection: 'facets',
      });


    Because you were importing payload

    inside a hook inside the config

    - it likely was interfering with the initialization of payload itself

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

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