3

I am trying to write an api by using redux-saga. I have my servicesSaga.js like this

import { FETCH_USER } from '../actions/actionTypes'
import { delay } from 'redux-saga'
import { call, put, takeEvery, takeLatest } from 'redux-saga/effects'
import { _getUserInformation } from './api'

const getUserInformation = function*(action) {
    console.log("FONKSİYONA geldi")
    console.log(action)
    try {
        console.log("try catche geldi")
        const result = yield call(_getUserInformation, action)
        console.log("result döndü")
        if (result === true) {
            yield put({ type: FETCH_USER })
        }
    } catch (error) {

    }
}

export function* watchGetUserInformation() {
    yield takeLatest(FETCH_USER, getUserInformation)
    console.log("WatchUsere geldi")
}

I am trying to yeild call my _getUserInformation method from ./api but yield call method is not working.This is my api.js.

const url = 'http://myreduxproject.herokuapp.com/kayitGetir'


function* _getUserInformation(user) {
    console.log("Apiye geldi" + user)
    const response = yield fetch(url, {
        method: 'POST',
        headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            email: user.email,
        })
    })

    console.log(response.data[0])
    return yield (response.status === 201)
}

export const api ={
    _getUserInformation
}

Thank you for your helps from now.

almtl
  • 95
  • 5
  • are you running your reduxSaga().run(YourSaga) after your store creation ? – Prakash Karena Jan 17 '20 at 05:34
  • Yeah console.log("try catche geldi") is working. While calling ""const result = yield call(_getUserInformation, action)"" dropping catch method – almtl Jan 17 '20 at 05:40

1 Answers1

0

generator function must be define as function* yourFunction() {} try this changes.

servicesSaga.js

function* getUserInformation(action) {
    try {
        const result = yield _getUserInformation(action) //pass user here
        if (result) {
            yield put({ type: FETCH_USER })
        }
    } catch (error) {

    }
}

export function* watchGetUserInformation() {
    yield takeLatest(FETCH_USER, getUserInformation)
}

api.js

    const url = 'http://myreduxproject.herokuapp.com/kayitGetir'

    function* _getUserInformation(user) {

        const response = yield fetch(url, {
            method: 'POST',
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                email: user.email,
            })
        })
        console.log('response',response);
        return response;
    }

export {
 _getUserInformation
}
Prakash Karena
  • 2,525
  • 1
  • 7
  • 16