代码之家  ›  专栏  ›  技术社区  ›  Sambulo Senda

未经处理的拒绝(错误):给定操作返回未定义的响应

  •  3
  • Sambulo Senda  · 技术社区  · 7 年前

    我不知道为什么会出错。未处理的拒绝(错误):给定操作“设置车辆”,减速器“车辆”返回未定义。要忽略某个操作,必须显式返回上一个状态。如果希望这个减缩器不包含任何值,可以返回null而不是undefined。

    减速器

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

    enter image description here

    2 回复  |  直到 7 年前
        1
  •  2
  •   kingdaro    7 年前

    ASI strikes again.

    return 
    action.items;
    

    这被解释为:

    return;
    action.items;
    

    把它们放在一条线上,它应该会起作用。我也会考虑使用格式化程序,比如 prettier 。这将帮助你自己和其他人查看你的代码,以便更容易地看到这样的错误。

        2
  •  1
  •   AndrewL64    7 年前

    你的 fetch 缺少一个 return 在它的fat arrow函数中,因此fetch不会返回任何内容。

    更改此行:

    .then(json => {
        this.props.setCars(json.results)
    })
    

    为此:

    .then(json => {
        return this.props.setCars(json.results)
    })