Can`t use custom restClient for replacing id to _id. Parsing error: Unexpected token
Can`t use custom restClient for replacing id to _id. Parsing error: Unexpected token
我正在尝试更改 restClient 以支持 mongoDB(将 id 替换为 _id)。
这是文档 https://marmelab.com/admin-on-rest/FAQ.html#can-i-have-custom-identifiers-primary-keys-for-my-resources。我尝试了不同的变体,但这个变体显示 "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>
当我使用 jsonServerRestClient() 时它起作用了。也许还有其他方法可以将 id 更改为 _id?
编辑后的答案:
您需要更换:
data: json.map(resource => { ...resource, id: resource._id }),
来自
data: json.map(resource => ({ ...resource, id: resource._id })),
注意返回对象周围的附加括号
我正在尝试更改 restClient 以支持 mongoDB(将 id 替换为 _id)。
这是文档 https://marmelab.com/admin-on-rest/FAQ.html#can-i-have-custom-identifiers-primary-keys-for-my-resources。我尝试了不同的变体,但这个变体显示 "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>
当我使用 jsonServerRestClient() 时它起作用了。也许还有其他方法可以将 id 更改为 _id?
编辑后的答案:
您需要更换:
data: json.map(resource => { ...resource, id: resource._id }),
来自
data: json.map(resource => ({ ...resource, id: resource._id })),
注意返回对象周围的附加括号