使用Mongoose和Redux删除记录

莱斯

当用户按下给定记录的按钮时,我想删除mongoDB中的记录。以供参考:

在此处输入图片说明

我已经设置了所有的猫鼬和redux代码,但出现此错误: 在此处输入图片说明

这是我的动作:

//deletes a survey when user clicks button
export const deleteSurvey = (surveyId) => async dispatch => {
  const response = await axios.delete("/api/surveys", surveyId);
  dispatch({ type: FETCH_SURVEYS, payload: response.data });
};

这是我的路线处理程序:

app.delete("/api/surveys", requireLogin, (req, res) => {
    Survey.findByIdAndRemove(req.surveyId, (err, survey) => {
      let response = {
        message: "Survey successfully deleted",
      };
      res.status(200).send(response);
    });
  });

记录列表在基于组件的react组件内部呈现。我正在这样导入我的动作:

import { deleteSurvey } from "../../actions";

并且该操作的调用是在按钮的onClick事件上触发的:

<button
    onClick={() => this.props.deleteSurvey(survey._id)}
    className="btn-floating btn-large red"
   >
   <i className="material-icons">clear</i>
</button>

我该如何解决此错误?

编辑:组件连接

function mapStateToProps({ surveys }) {
  return { surveys }; //returns the surveys reducer from the combined reducers
}

export default connect(mapStateToProps, { fetchSurveys })(SurveyList);

编辑:新错误消息

在此处输入图片说明

指的是:

return this.props.surveys.map(survey => {...}

编辑:从API获取调查

const surveys = await Survey.find({ _user: req.user.id }) //get all surveys for the current user
      .select({ recipients: false }); //dont include recipients
    res.send(surveys);
linasmnew

您尚未将操作连接到组件:

export default connect(mapStateToProps, { fetchSurveys, deleteSurvey })(SurveyList);

编辑:您的第二个错误是由于映射仅适用于数组,因此您需要确认这surveys是可以使用的数组:console.log(this.props.surveys)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章