在axios.delete()上获取404(未找到) [英] Getting 404 (Not Found) on axios.delete()

查看:172
本文介绍了在axios.delete()上获取404(未找到)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的调查创建应用程序实现删除功能.

I'm trying to implement the delete functionality for my survey creation app.

我正在将MongoDB和mongoose用于数据库,将node.js/Express用于后端服务器,将React/Redux用于前端.

I'm using MongoDB with mongoose for the database, node.js/Express for the backend server and React/Redux for the frontend side.

尽管我认为我正确设置了路由,但在axios.delete()上却得到404(未找到).

Although I think I set routing correctly, I get 404 (Not Found) on axios.delete().

该错误表明未找到 http://localhost:3000/api/surveys/delete/{the-survey-id-here} .

我一直在阅读axios,Express,猫鼬和其他网站的文档,但是,对我没有任何帮助.

I have been reading the documentation of axios, Express, mongoose and other websites, however, nothing worked for me.

我尝试了以下操作.

  • 使用findByIdAndRemove()而不是deleteOne()
  • 在动作创建者中
  • 传递 surveyId const响应=等待axios.delete("/api/surveys",数据:{surveyId});
  • 使用< a></a> 代替< button></button>
  • Use findByIdAndRemove() instead of deleteOne()
  • Pass surveyId in the action creator const response = await axios.delete("/api/surveys", data: { surveyId });
  • Use <a></a> instead of <button></button>

这是我的密码.

SurveyList.js(具有删除按钮的反应组件)

SurveyList.js (react component which has the delete button)

import { fetchSurveys, deleteSurvey } from '../../actions'

...

<div className="card-action">
  <a className="red-text text-accent-1">Yes: {survey.yes}</a>
  <a className="red-text text-accent-1">No: {survey.no}</a>
  <button
    className="btn-floating btn-small waves-effect waves-light red right"
    onClick={() => this.props.deleteSurvey(survey._id)}
  >
    <i className="material-icons">delete_forever</i>
  </button>
</div>

...

const mapStateToProps = ({ surveys }) => {
  return { surveys }
}

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

actions/index.js(动作创建者)

actions/index.js (action creator)

export const deleteSurvey = (surveyId) => async dispatch => {
  const response = await axios.delete(`/api/surveys/delete/${surveyId}`)

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

surveyRoute.js(路由处理程序)

surveyRoute.js (routing handler)

app.delete('/api/surveys/delete/:surveyId', async (req, res) => {
  await Survey.deleteOne({ _id: req.params.surveyId })

  const surveys = await Survey.find({ _user: req.user.id }).select({
    recipients: false
  })
  res.send(surveys)
})

server/index.js

server/index.js

const express = require('express')
const mongoose = require('mongoose')
const cookieSession = require('cookie-session')
const passport = require('passport')
const bodyParser = require('body-parser')
const keys = require('./config/keys')
require('./models/User')
require('./models/Survey')
require('./services/passport')

mongoose.connect(keys.mongoURI)

const app = express()

app.use(bodyParser.json())
app.use(
  cookieSession({
    maxAge: 30 * 24 * 60 * 60 * 1000, // milliseconds
    keys: [keys.cookieKey]
  })
)
app.use(passport.initialize())
app.use(passport.session())

require('./routes/authRoutes')(app)
require('./routes/billingRoutes')(app)
require('./routes/surveyRoutes')(app)

if(process.env.NODE_ENV === 'production'){
  app.use(express.static('client/build'))

  const path = require('path')
  app.get('*',(req, res) => {
    res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
  })
}

const PORT = process.env.PORT || 5000
app.listen(PORT)

推荐答案

In my case, leads/ is the end point. try your end point + survey id
With axios@^0.19.2

       axios
            .delete('/api/leads/' + id)
            .then(res => {
                dispatch({
                    type: DELETE_LEAD,
                    payload: id
                });

这篇关于在axios.delete()上获取404(未找到)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆