0

I`m trying to change restClient to support mongoDB(replace id to _id).

This is documantation https://marmelab.com/admin-on-rest/FAQ.html#can-i-have-custom-identifiers-primary-keys-for-my-resources. I tried different variants, but with this one it shows "Unexpected token"

This is the screenshot of error

const convertHTTPResponseToREST = (response, type, resource, params) => {
        const { headers, json } = response;
        switch (type) {
            case GET_LIST:
                return {
                    data: json.map(resource => { ...resource, id: resource._id } ), // here an error with "...resource"
                    total: parseInt(headers.get('content-range').split('/').pop(), 10),
                };
            case UPDATE:
            case DELETE:
            case GET_ONE:
                return { ...json, id: json._id };
            case CREATE:
                return { ...params.data, id: json._id };
            default:
                return json;
        }
    };


 <Admin
         title="Dashboard"
         restClient={convertHTTPResponseToREST('/api')}>
 </Admin>

When I use jsonServerRestClient() it works. Maybe there is other way to change id to _id?

  • [answer](https://stackoverflow.com/a/33745466/6124657) - admin-on-rest outdated, use react-admin ... use graphql (over/instead rest) – xadm May 26 '19 at 22:32
  • Property 'restClient' does not exist on type 'IntrinsicAttributes & AdminProps error is apper on my document. Do you know the reason? – sayinmehmet47 Feb 08 '22 at 11:56

1 Answers1

1

Edited answer:

You need to replace:

data: json.map(resource => { ...resource, id: resource._id }),

by

data: json.map(resource => ({ ...resource, id: resource._id })),

Note the additional parenthesis around the returned object

Gildas Garcia
  • 6,966
  • 3
  • 15
  • 29
  • Actually this problem is not connected with the spread operator because this error appears only on that line. And when I changed values everything was ok, but functionality didn`t work properly. This question is more connected with admin-on-rest. – Oleg Chobotar May 27 '19 at 09:04
  • Edited my answer – Gildas Garcia May 27 '19 at 13:05