lexich/redux-api

Concurrent call on CRUD endpoint issue

Open

#158 创建于 2017年7月18日

在 GitHub 查看
 (3 评论) (0 反应) (0 负责人)JavaScript (496 star) (95 fork)batch import
help wanted

描述

Hi,

I'm not sure I can quickly create reproducible example, but here is the case.

I have an endpoint defined: registrations: { url: `/api/registrations`, crud: true, transformer: transformers.array },

In one place I'm using get() to get the data:

        console.log("Getting data...");
        dispatch(rest.actions.registrations.get({}, {}, (err, data) => {
            console.log("SETTING STATE", data);
            this.setState({
                registrations: data
            })
        }));

(the original code was simpler, I was simply using props.registration.data, but then, investigating this issue, created this)

In another place (modal window) I do POST request for this endpoint:

this.props.dispatch(rest.actions.registrations.post({}, { body: JSON.stringify(data) }, (err, data) => {
            if (err != null) {
                console.log("Call failed: ", err);
                this.setState({ error: err.message });
                return
            }
        }))

So, when POST request happens, data for the first GET request is also changed. In fact I see in console log this:

Getting data...
SETTING STATE > [Object] // which is valid data obtained with GET request
// ... modal window appears and sends POST request ...
SETTING STATE > [Object] // with object equal to result of POST: { status: "success" }

Note, that after POST request there is no "Getting data..." log entry. It looks like callback from the first action dispatch got called automagically by itself after POST request.

Any idea what I'm doing wrong here?

贡献者指南