react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

Options is undefined when using httpClient

Open remi13131 opened this issue 9 years ago • 1 comments

EDIT for tl;dr: See comment below, it is not an issue in the end, but more a forgotten thing in the docs. Sorry but I can't close it myself nor delete it. (eg https://stackoverflow.com/questions/3081521/how-to-completely-remove-an-issue-from-github) Sorry for the inconvenience.


I am trying to implement Authentication/Authorization in my AOR 0.9.3 project. I based my logic on this sample code from documentation : https://marmelab.com/admin-on-rest/Authentication.html#sending-credentials-to-the-rest-api Only difference is jsonServerRestClient instead of simpleRestClient. It is the provided one and not a custom one.

here is the declaration :

const httpClient = (url, options) => {
    if (!options.headers) {
        options.headers = new Headers({ Accept: 'application/json' });
    }
    const token = localStorage.getItem('token');
    options.headers.set('Api-Token', token);
    return fetchUtils.fetchJson(url, options);
}
const restClient = jsonServerRestClient('http://localhost:8000/api/v1', httpClient);

class App extends Component {
    render() {
        return (
            <Admin title="CMDB Frontend" restClient={restClient} authClient={authClient} >
                [... No relevant code here ...]
            </Admin>
        );
    }
}
export default App;

All works well and I am able to protect the views that I want.

However, in all pages that I navigate to where a ReferenceField is used, I get "options is undefined" in a red box showing at the bottom of the page. In other pages all seems to work as expected. aor-bug-optionsisundefined

As a result the references doesn't fetch properly (It fetches some of the references, I don't know on which criteria, but only few of them and always the same ones)

What's more Firefox ErrorLog doesn't help me much : aor-bug-optionsisundefined-errorlog

Is this a bug or am I missing something ? Sorry if this does not belong to here and should be on SO, I really hesitated on where to post this...

remi13131 avatar Mar 23 '17 11:03 remi13131

Okay... adding default value to options do the trick... Sorry for the inconvenience, admin can close this non-issue now.

But I think you should update doc to integrate this

const httpClient = (url, options = {}) => {
[...]
}

instead of

const httpClient = (url, options) => {
[...]
}

Made me loose 1 to 2 hours !

remi13131 avatar Mar 23 '17 13:03 remi13131