how to count the number

default discord avatar
clhome
last month
1 1

Hello everyone, I am a newbie
in collections,I create a c.ts,it contain C1,C2,C3,Now I wanna make c3=c1+c2,how to get c1,c2 value in c3's hook, pls help to how to do it,thanks,
my code is below:
`
import { CollectionConfig, Field } from 'payload/types';

const Tags: CollectionConfig = {
slug: 'c_sum',

admin: {
useAsTitle: 'name',
defaultColumns: ['name', 'c1','c2','c_sum']
},
access: {
read: () => true,
create:()=>true,
},
fields: [
{
type: 'text',
name: 'name',
},
{
type: 'number',
name: 'c1',
label: "c1",
},
{
type: 'number',
name: 'c2',
label: "c2",
},
{
type: 'number',
name: 'c_sum',
label: "c_sum",
},

],
timestamps: false,
}
export default Tags;`

  • default discord avatar
    clhome
    last month

    i solved:

    {
          type: 'number',
          name: 'c_sum',
          label: "c_sum",
          admin: {
            description: 'c1+c2',
            hidden:false
            
          },
          access: {
            update: () => false,
            create:()=>false
            
          },
          hooks: {
            afterRead: [
              ({ data }) => {  
                return data.c1 + data.c2;            
              }
            ],
          },
        },
Open the post
Continue the discussion in GitHub
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.