There is an endpoint where I can create new a Post, sending data via FormData().
There is a rule on my API, which allows the user creates only ONE post per day.
I got no problems on the first time I create a new post, but when I try to create a second one on the same day, to fall on the catch block and show to the user an error, I got this:
[TypeError: undefined is not an object (evaluating '_yield$call.data')]
posts/sagas.ts
import { call, put } from 'redux-saga/effects'import axios, { AxiosResponse, AxiosError } from 'axios';function apiPostRequestPost({ image, subtitle, latitude, longitude }: ApiPostRequestPost) { const form = new FormData(); const filename = image.uri.split('/').pop(); const match = /\.(\w+)$/.exec(filename!!); const type = match ? `image/${match[1]}` : `image`; const latitudeAsString = String(latitude); const longitudeAsString = String(longitude); form.append('photo', { uri: image.uri, name: image.fileName, type }); form.append('subtitle', subtitle || ''); form.append('latitude', latitudeAsString); form.append('longitude', longitudeAsString); return api.post('/posts', form, { headers: {'Accept': 'application/json','Content-Type': 'multipart/form-data', } });}export function* addPost({ payload }: CreatePostAction) { try { const { data }: AxiosResponse<Post> = yield call(apiPostRequestPost, payload); yield put(addPostSuccess(data)); Toast.show({ type: 'success', text1: 'Seu post foi publicado com sucesso!', position: 'bottom', }); } catch (error) { console.log({error}); if (axios.isAxiosError(error)) { console.log(error.response) Toast.show({ type: 'error', text1: `${error.response?.data?.message} 😥`, }); } const err = error as AxiosError<{ error: string }>; Toast.show({ type: 'error', text1: `${err.message} 😥`, }); yield put(addPostFailure()); }}
I've tried to add async statement to apiPostRequestPost function, like this:
async function apiPostRequestPost({ image, subtitle, latitude, longitude }: ApiPostRequestPost) {...}
Also found these questionsyield call returning undefined rather than resolved promise value,React Saga Generator yield call undefined objectwhich are pretty similar to my problem, but not my case...