Can`t use custom restClient for replacing id to _id. Parsing error: Unexpected token

307 views Asked by At

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?

1

There are 1 answers

2
Gildas Garcia On BEST ANSWER

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