gpt4 book ai didi

reactjs - react native 传奇 yield 调用不起作用

转载 作者:行者123 更新时间:2023-12-05 07:14:43 25 4
gpt4 key购买 nike

我正在尝试使用 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 方法,但调用方法不起作用。这是我的 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
}

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

最佳答案

生成器函数必须定义为 function* yourFunction() {} 试试这个改变。

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
}

关于reactjs - react native 传奇 yield 调用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59781543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com