afterChange `doc` depth is different when calling from local API than from upload in dashboard.

default discord avatar
twoxic
5 months ago
2

When running from dashboard AfterChange

doc

parameter returns object with

depth: 0

and when running from the local API the depth is

infinite?

. E.g.:



1. Afterchange

doc

when item is created in the dashboard.


{
    id: '643ff434dd36a0f405269545',
    geojson_file: '643ff426dd36a0f405269510',
    type: 'annotation',
    location: '643ff3c6a997b34ac18de3a1',
    is_impartial: false,
    orthophoto: '643ff3bca997b34ac18de2e7',
    species: '643ff3c6a997b34ac18de393',
    createdAt: '2023-04-19T14:01:24.792Z',
    updatedAt: '2023-04-19T14:01:24.792Z'
}


2. Afterchange

doc

when item is created by the local API (seed script).



{
  id: '643ff4d0ce7ee4e37b366562',
    geojson_file: {
    id: '643ff4d0ce7ee4e37b366560',
      filename: 'redacted.geojson',
      ...more
  },
  type: 'prediction',
    location: {
    id: '643ff4cdce7ee4e37b3664db',
      name: 'redacted',
      image: {
      id: '643ff4cdce7ee4e37b3664d9',
        name: 'redacted',
        ...more
    },
    extent: { type: 'Feature', properties: {}, geometry: [Object] },
    surface_area_ha: 188,
      n_analyses: 2,
      ...more
  },
  is_impartial: true,
    orthophoto: {
    id: '643ff4cece7ee4e37b3664fc',
      name: 'redacted',
      ...more
  },
  species: {
    id: '643ff4cece7ee4e37b3664f5',
      name: 'redacted',
      image: {
        id: '643ff4cece7ee4e37b3664f3',
        ...more
    }
  },
  createdAt: '2023-04-19T14:04:00.817Z',
  updatedAt: '2023-04-19T14:04:00.817Z'
}


Seed code:


await payload.create({
    collection: 'analyses',
    data: {
        ...params,
        type: 'prediction',
        geojson_file: geojson.id
    }
});


This results in breaking the

afterChange

function since I call a find on my

geojson_file

like

id: doc.geojson_file

. However, when seeding this is not necessary (and possible) since it is already included. What's the intended behaviour here and how to fix?



In case this is not intended behaviour I'm happy to create a Github issue and a reproducible repo.

  • discord user avatar
    jesschow
    Payload Team
    2 months ago

    Hey @twoxic did you end up opening an issue for this one?

  • default discord avatar
    twoxic
    2 months ago

    No, I ended up just writing a utility function that checks whether or not the value is an object (the resource itself) or a string (the ID), and if it is the latter fetch it and return it



    import payload from 'payload';
    import { Config } from 'payload/generated-types';
    import { isString } from './isString';
    
    export const getResource = <CollectionType>(
      resourceOrId,
      collection: keyof Config['collections']
    ): Promise<CollectionType> => {
      // doc.geojson_file could be either string (ID) or the object itself.
      // See: https://discord.com/channels/967097582721572934/1098250710547710092/1098250710547710092
      if (!isString(resourceOrId)) {
        return resourceOrId;
      }
    
      // @ts-ignore
      return payload.findByID({
        collection,
        id: resourceOrId
      });
    };


    Note: The typings are not 100% correct (e.g.: missing Promise)

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.