向Axios发出一个项目的GET请求时,该对象返回null

时间:2017-06-21 03:02:32

标签: node.js reactjs react-redux axios

我可以确认我可以获得所有项目,但无论出于何种原因,当我定位特定ID时,它返回null  在我的控制器内



export function getCourse(id) {
  return function(dispatch) {
    axios.get(`http://localhost:3001/courses/${id}`)
    .then((response) => {
      console.log(response.data)
      dispatch({
        type: GET_COURSE,
        payload: response.data.course
      })
      console.log(response.data)
    })
  }
}




在动作创作者中



exports.getCourse = function(req, res) {
  Course.findOne({ cuid: req.params.cuid }).exec((err, course) => {
    if (err) {
      res.status(500).send(err);
    }
     return res.json({ course });
  });
};




在我的路线内



router.route('/courses/:cuid').get(CourseController.getCourse);



 减速机里面......


import { ADD_COURSE, GET_COURSES, GET_COURSE } from '../actions/types';

const INITIAL_STATE = {
  coursename: [],
  _creator: {},
  courses: {},
  course: {}
};

export default function(state = INITIAL_STATE, action) {
  switch (action.type) {
    case ADD_COURSE:
      return {
        ...state,
        coursename: [...state.coursename, action.coursename],
        _creator: action._creator,
      };
    case GET_COURSES:
      return {
        ...state,
        courses: action.payload
      }
     case GET_COURSE:
     console.log(state.course)
      return {
        ...state,
        course: action.payload
      }
  }
  return state;
}




最后,在我的组件



componentDidMount() {
    this.props.dispatch(actions.getCourse(this.props.match.params.cuid));
    console.log(this.props.course, "Is this it")
  }




0 个答案:

没有答案