Javascript 未处理的拒绝(错误):给定操作返回未定义的
我不确定我为什么会出错。未处理的拒绝(错误):给定操作“设置车辆”,减速器“车辆”返回未定义。若要忽略操作,必须显式返回上一个状态。如果希望此减缩器不包含任何值,可以返回null而不是undefined 还原剂Javascript 未处理的拒绝(错误):给定操作返回未定义的,javascript,reactjs,redux,Javascript,Reactjs,Redux,我不确定我为什么会出错。未处理的拒绝(错误):给定操作“设置车辆”,减速器“车辆”返回未定义。若要忽略操作,必须显式返回上一个状态。如果希望此减缩器不包含任何值,可以返回null而不是undefined 还原剂 import { SET_CARS} from "../actions"; import { combineReducer } from "redux"; function cars(state = [], action) { switch (action.type)
import { SET_CARS} from "../actions";
import { combineReducer } from "redux";
function cars(state = [], action) {
switch (action.type) {
case SET_CARS:
return
action.items;
default:
return state; }
}
const rootReducer = combineReducers({
cars
});
export default rootReducer;
行动
export const SET_CARS = 'SET_CARS';
export function setCars(items){
return {
type: SET_CARS ,
items
}
}
搜索车
class SearchCars extends Component {
constructor() {
super();
this.state = {
tansmition: ""
};
}
search() {
let { tansmition } = this.state;
const url = `http://localhost:4000/vehicles/?
transmission=${tansmition}`;
fetch(url, {
method: 'GET'
})
.then(response => response.json())
.then(json => {
this.props.setCars(json.results)
})
}
您的
提取
在其胖箭头函数中缺少一个返回
,因此提取不会返回任何内容
更改此行:
.then(json => {
this.props.setCars(json.results)
})
为此:
.then(json => {
return this.props.setCars(json.results)
})
这被解释为:
return;
action.items;
把它们放在一条线上,它应该会起作用。我也会考虑使用格式化程序。这将帮助您自己和其他人更容易地查看代码中的bug。.then(json=>{返回this.props.setCars(json.results)})。还是会出错
return;
action.items;