React native saga yiga call无法使用

问题描述 投票:0回答:1

我正在尝试使用redux-saga编写api。我有这样的我的servicesSaga.js

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")
}

[我试图从./api中调用_getUserInformation方法,但是yield调用方法不起作用。这是我的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
}

感谢您从现在开始的帮助。

reactjs react-native react-redux redux-saga saga
1个回答
0
投票
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 }

© www.soinside.com 2019 - 2024. All rights reserved.