Saturday, July 17, 2021

React-admin with typeORM

1. React-admin missing X-total-count issue.
Need to fix it from server side response header in order to meet react-admin requirement.

To fix it, add extra response header: Category.tsx

 // GET ALL
  @Get('')
  public async getAll() {
    //return getCategory();
    let objs = await getCategory();
    if (Array.isArray(objs)) {
      let total = objs.length;
      // react-admin
      this.setHeader('Access-Control-Expose-Headers''X-Total-Count')
      this.setHeader('X-Total-Count'total+"")
    }
    return objs;
  }

 

2. Unable to update data if primary id also submit from data. typeORM only take ID from URL parameter, not from data.

[1] GET /api/categories?_end=10&_order=ASC&_sort=id&_start=0 200 1.591 ms - 390
[1] Caught Validation Error for /api/categories/undefined: { categoryId: { message: 'invalid float number', value: 'undefined' } }
[1] GET /api/categories/undefined 422 0.702 ms - 176

To fix it, tailor the submission data: category.router.ts

export const CategoryEdit = (props:any=>{
    //typeORM doesn't like to have id in the submission data again.
    const transform = (data:any=> {
        const {id, ...newData} = data;
        return newData;
    };
    return (
        <Edit title="Edit Category" {...props} transform={transform}>
            <SimpleForm>
                <TextInput source="name" />
            </SimpleForm>
        </Edit>
    )
};




No comments: