javascript - Can't display data from REST call, even though the server is sending data -
i trying perform rest call token in header display information. there required header token code looks restclient.js, app.js, , users.js.
//restclient.js import { jsonserverrestclient, fetchutils } 'admin-on-rest'; const httpclient = (url, options = {}) => { if (!options.headers) { options.headers = new headers({ accept: 'application/json' }); } options.headers.set('token', 'admin'); return fetchutils.fetchjson(url, options); } const restclient = jsonserverrestclient('http://localhost:8080/api/v2', httpclient); export default (type, resource, params) => new promise(resolve => settimeout(() => resolve(restclient(type, resource, params)), 500)); //app.js import react, {component} 'react'; import { admin, resource } 'admin-on-rest'; import { userlist } './users'; import restclient './restclient'; class app extends component { render() { return( <admin restclient={restclient}> <resource name="admin/user" list={userlist}/> </admin> ); } } export default app; //users.js // in src/users.js import react 'react'; import { list, datagrid, emailfield, textfield, simplelist } 'admin-on-rest'; export const userlist = (props) => ( <list {...props}> <datagrid > <textfield source="email"/> <textfield source="info"/> </datagrid> </list> );
i've tested rest call postman , returning data. there anyway check data being sent in call? server running express.js , i've set route include required headers.i've attached example of json looks returning.
since aor fetchutils returns promise. can intercept promise , perform kind of inspection want (and lot more too)
below how code handling similar. intercepting api call , displaying custom notifications.
function handlerequestandresponse(url, options={}) { return fetchutils.fetchjson(url, options) .then((response) => { const {headers, json} = response; //admin on rest needs {data} key const data = {data: json} if (headers.get('x-total-count')) { data.total = parseint(headers.get('x-total-count').split('/').pop(), 10) } // handle get_list responses if (!isnan(parseint(headers.get('x-total-count'), 10))) { return {data: json, total: parseint(headers.get('x-total-count').split('/').pop(), 10)} } else { return data } }) }
you can below
return fetchutils.fetchjson(url, options) .then(res => { console.log(res) return res })
Comments
Post a Comment