简体   繁体   中英

Issues with post request using redux-saga

I make a post request in my application, using redux saga middleware. Now, the code related to my post request looks like this:

 function* postNewMessage(newMessage) { console.log(newMessage) const {var1, var2} = newMessage; try { const data = yield call(() => { return fetch(myapi, { method: 'POST', headers: { Accept: `application/json`, 'Content-Type': 'application/json', }, body: JSON.stringify({ first: var1, second: var2 }), }) }); console.log(data); } catch (error) { console.log(error); } } function* addNewMessageSaga(action) { console.log(action) try { yield postNewMessage(action.newMessage) }catch (e) { console.log(e) } } function* watchNewMessage() { takeEvery(POST_MESSAGE, addNewMessageSaga) } //bellow is my action which i call in my component export const postMessage = (newMessage) => { console.log(newMessage) return { type: POST_MESSAGE, newMessage }; };

This is all my code related with post request. Now i call the action inside component like:

 postMessageOnApi({ var1: selector.data, var2:selector.data1[0] })};

When i click on the button to post my data, i get the values only inside postMessageOnApi , there console.log() shows my values, but in function * postNewMessage.. i don't get data.
Who knows where i made a mistake and how to solve the issue?

It looks like you forgot to use yield before takeEvery

function* watchNewMessage() {
    yield takeEvery(POST_MESSAGE, addNewMessageSaga)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM