rick & morty api 对具有 id 的角色的请求有问题
rick & morty api problem with request on character with an id
我想提出一个关于 api 瑞克和莫蒂的请求。要获取角色的详细信息,您必须指定一个id作为参数:"https://rickandmortyapi.com/api/character/id"
我使用 redux-saga 来获取我的请求,但请求失败并显示一条消息:
" 嘿!不允许使用该参数,请尝试使用数字代替;)"
我尝试使用我的组件中收到的 id 请求,请求有效。我认为它要么来自我的行动,要么来自我的传奇......
这是我的操作:
export const getCharactersDetails = () => {
return { type: GET_CHARACTER_DETAILS }
}
这是我的故事:
const getLatestDetails = (id) =>
fetch('https://rickandmortyapi.com/api/character/' + id);
export function* fetchCharacterDetails() {
try {
let myCharacter;
const response = yield call(getLatestDetails);
const result = yield response.json();
if (result.error) {
yield put({ type: DETAILS_REQUEST_FAILED, error: result.error });
} else {
myCharacters = result.results
yield put({ type: DETAILS_RECEIVED, character: myCharacter });
}
} catch (error) {
yield put({ type: DETAILS_REQUEST_FAILED, error: error.message });
}
}
function* actionWatcher() {
yield takeLatest(GET_CHARACTERS, fetchCharacters)
yield takeLatest(GET_CHARACTER_DETAILS, fetchCharacterDetails)
}
我这样称呼我的动作:
let CharacterId = this.props.navigation.state.params.id
this.props.dispatch(getCharactersDetails(CharacterId))
知道我做错了什么吗?
您没有将任何 ID 传递给您的 getLatestDetails
函数。
将 ID 添加到您正在创建的操作中:
export const getCharactersDetails = id => {
return { type: GET_CHARACTER_DETAILS, id }
}
…然后将动作中的 ID 作为参数添加到您的 saga 并将其作为第二个参数传递给您的 call
效果:
export function* fetchCharacterDetails({ id }) {
try {
let myCharacter;
const response = yield call(getLatestDetails, id);
const result = response.json();
if (result.error) {
yield put({ type: DETAILS_REQUEST_FAILED, error: result.error });
} else {
myCharacters = result.results
yield put({ type: DETAILS_RECEIVED, character: myCharacter });
}
} catch (error) {
yield put({ type: DETAILS_REQUEST_FAILED, error: error.message });
}
}
补充说明:
- saga 将动作作为参数;我们在这里使用解构来获取 ID(注意花括号)
call
接受要调用的函数作为第一个参数,然后是传递给函数的其他参数,这就是 getLatestDetails
获取 ID 的方式
- 我在
response.json()
之前删除了yield
,这只是一个普通的旧同步操作
我想提出一个关于 api 瑞克和莫蒂的请求。要获取角色的详细信息,您必须指定一个id作为参数:"https://rickandmortyapi.com/api/character/id"
我使用 redux-saga 来获取我的请求,但请求失败并显示一条消息: " 嘿!不允许使用该参数,请尝试使用数字代替;)"
我尝试使用我的组件中收到的 id 请求,请求有效。我认为它要么来自我的行动,要么来自我的传奇......
这是我的操作:
export const getCharactersDetails = () => {
return { type: GET_CHARACTER_DETAILS }
}
这是我的故事:
const getLatestDetails = (id) =>
fetch('https://rickandmortyapi.com/api/character/' + id);
export function* fetchCharacterDetails() {
try {
let myCharacter;
const response = yield call(getLatestDetails);
const result = yield response.json();
if (result.error) {
yield put({ type: DETAILS_REQUEST_FAILED, error: result.error });
} else {
myCharacters = result.results
yield put({ type: DETAILS_RECEIVED, character: myCharacter });
}
} catch (error) {
yield put({ type: DETAILS_REQUEST_FAILED, error: error.message });
}
}
function* actionWatcher() {
yield takeLatest(GET_CHARACTERS, fetchCharacters)
yield takeLatest(GET_CHARACTER_DETAILS, fetchCharacterDetails)
}
我这样称呼我的动作:
let CharacterId = this.props.navigation.state.params.id
this.props.dispatch(getCharactersDetails(CharacterId))
知道我做错了什么吗?
您没有将任何 ID 传递给您的 getLatestDetails
函数。
将 ID 添加到您正在创建的操作中:
export const getCharactersDetails = id => {
return { type: GET_CHARACTER_DETAILS, id }
}
…然后将动作中的 ID 作为参数添加到您的 saga 并将其作为第二个参数传递给您的 call
效果:
export function* fetchCharacterDetails({ id }) {
try {
let myCharacter;
const response = yield call(getLatestDetails, id);
const result = response.json();
if (result.error) {
yield put({ type: DETAILS_REQUEST_FAILED, error: result.error });
} else {
myCharacters = result.results
yield put({ type: DETAILS_RECEIVED, character: myCharacter });
}
} catch (error) {
yield put({ type: DETAILS_REQUEST_FAILED, error: error.message });
}
}
补充说明:
- saga 将动作作为参数;我们在这里使用解构来获取 ID(注意花括号)
call
接受要调用的函数作为第一个参数,然后是传递给函数的其他参数,这就是getLatestDetails
获取 ID 的方式
- 我在
response.json()
之前删除了yield
,这只是一个普通的旧同步操作