【发布时间】:2018-05-10 00:10:42
【问题描述】:
我正在尝试使用堆栈 react redux 和 redux-saga 并了解所需的最少管道。
我做了一个 github repo 来重现我得到的错误:
- https://github.com/kasra0/react-redux-saga-test.git
- 运行应用程序:npm run app
- 网址:http://localhost:3000/
该应用由一个简单的组合框和一个按钮组成。 从组合中选择一个值后,单击按钮会调度一个操作,该操作仅包含获取一些 json 数据。 服务器收到正确的请求(基于选定的值),但在 let json = yield call([res, 'json']) 这一行。我得到了错误:
我从浏览器得到的错误信息:
index.js:2177 uncaught at equipments at equipments
at takeEvery
at _callee
SyntaxError: Unexpected end of input
at runCallEffect (http://localhost:3000/static/js/bundle.js:59337:19)
at runEffect (http://localhost:3000/static/js/bundle.js:59259:648)
at next (http://localhost:3000/static/js/bundle.js:59139:9)
at currCb (http://localhost:3000/static/js/bundle.js:59212:7)
at <anonymous>
它来自我的一个传奇:
import {call,takeEvery,apply,take} from 'redux-saga/effects'
import action_types from '../redux/actions/action_types'
let process_equipments = function* (...args){
let {department} = args[0]
let fetch_url = `http://localhost:3001/equipments/${department}`
console.log('fetch url : ',fetch_url)
let res = yield call(fetch,fetch_url, {mode: 'no-cors'})
let json = yield call([res, 'json'])
// -> this is the line where something wrong happens
}
export function* equipments(){
yield takeEvery(action_types.EQUIPMENTS,process_equipments)
}
我在管道中做错了,但我找不到哪里。
非常感谢您的帮助!
卡斯拉
【问题讨论】:
-
很可能与响应结果不是 JSON 格式有关。试试
yield call([res, 'text']),看看实际效果如何。 -
(或查看您的网络标签)
-
嗨,谢谢你的线索。我检查了网络以查看响应的内容类型,并且确实收到了 application/json 。我刚刚使用了 then yield call([res, 'text']) 并且它没有错误地通过,但是即使我有响应,console.log(json) 也是空的
标签: redux redux-saga