axios删除功能不起作用

时间:2018-04-18 01:56:45

标签: reactjs express axios

我正在尝试创建一个删除按钮,在我的调查中我有删除按钮和我的结束api创建但是当我在调查中点击删除时没有任何反应请帮助。

这是我的删除按钮和功能所在的位置。  创建了一个名为newDelete的新函数,并将其传递给survey._id,但这不起作用

SurveyList.js

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { fetchSurveys } from '../../actions';
import { deleteSurvey } from '../../actions';

class SurveyList extends Component {
  componentDidMount() {
    this.props.fetchSurveys();
  }

  newDelete() {
  deleteSurvey('{survey._id}');
  }

  renderSurveys() {
    return this.props.surveys.reverse().map(survey => {
      return (
        <div className="card darken-1" key={survey._id}>
          <div className="card-content">
            <span className="card-title">{survey.title}</span>
            <p>
              {survey.body}
            </p>
            <p className="right">
            <button type="button" onClick={this.newDelete}>DELETE</button>
              Sent On: {new Date(survey.dateSent).toLocaleDateString()}
            </p>
          </div>

        </div>
      );
    });
  }

  render() {
    return (
      <div>
        {this.renderSurveys()}
      </div>
    );
  }
}

function mapStateToProps({ surveys }) {
  return { surveys };
}

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

我有我的api's。

index.js 

import axios from "axios";
import { FETCH_USER, FETCH_SURVEYS } from "./types";


export const fetchUser = () => async dispatch => {
  const res = await axios.get("/api/current_user");

  dispatch({ type: FETCH_USER, payload: res.data });
};

export const submitSurvey = (values, history) => async dispatch => {
  const res = await axios.post("/api/surveys", values);

  history.push("/surveys");
  dispatch({ type: FETCH_USER, payload: res.data });
};

export const fetchSurveys = () => async dispatch => {
  const res = await axios.get('/api/surveys');

    dispatch({type: FETCH_SURVEYS, payload: res.data })
};

export const deleteSurvey = (values, history) => async dispatch => {
  const res = await axios.delete('api/surveys', {params: {'_id': values}});

  history.push("/surveys");
  dispatch({ type: FETCH_USER, payload: res.data });
};

这是我试图删除调查的代码的一部分。任何帮助将不胜感激,谢谢。没有任何错误或任何控制台问题。

0 个答案:

没有答案