简体   繁体   English

redux 请求失败,状态码为 400(错误请求)

[英]redux request failed with status code 400(bad request)

i am getting an error "400 bad request" i can't find out what is my error below i share my frontend and backend code.. i am also share my image error link that i came https://ibb.co/swQPgYG我收到一个错误“400 bad request”我在下面找不到我的错误我分享了我的前端和后端代码..我还分享了我来的图像错误链接https://ibb.co/swQPgYG

@@@@@@@@@@@@@@@@@@@ backend @@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@后端@@@@@@@@@@@@@@@@@@@@@@

todoschema.js todoschema.js

this is a todoschema这是一个 todoschema

var mongoose = require('mongoose');

var todoSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true,
        maxlength:32,
        trim:true
    }
},{timestamps:true}
)

module.exports = mongoose.model('Todo',todoSchema)

auth.js/router auth.js/路由器

var express = require('express');
var router = express.Router();
const {addTodo} = require('../controller/auth');

router.post('/addtodo',addTodo);

module.exports = router;

auth.js/controller auth.js/控制器

 const Todo = require('../model/todo');
exports.addTodo = (req,res) =>{
  const todo = new Todo(req.body)
  todo.save((err,todo) => {
    if(err || !todo){
      return res.status(400).json({
          err : 'NOT able to store data in database'
      })
    }
    res.json(todo);
  })
}

################## frontEnd ########################### ################## 前端 ###########################

API == http://localhost:8000/api/ API == http://localhost:8000/api/

here i fetch request from my backend在这里我从我的后端获取请求

index.js index.js

import {API} from '../backend'
import axios from 'axios'
export const getdata = (todo) => {
    return (dispatch) => {
        axios.post(`${API}addtodo`)
        .then(res => {
            console.log(res)
            dispatch({
                type : 'FETCH_TODO',
                payload : todo
            })
        })
        .catch(err =>{
            console.log(err);
        })
    }
}

This is the todoForm where i add my todo这是我添加待办事项的 todoForm

todoform.js todoform.js

import React,{useState, useEffect} from 'react'
import '../App.css'
import {
    FormGroup,
    Input,
    Button,
    Form,
    InputGroup,
    InputGroupAddon
} from 'reactstrap';
import {v4} from 'uuid';
import 'bootstrap/dist/css/bootstrap.min.css';
import {getdata } from '../Auth'

//redux
import {connect, useDispatch} from 'react-redux'
import {addTodo} from '../Action/todo';
const TodoForm  = ({addTodo}) => {

    const [title,setTitle] = useState('')

    const dispatch = useDispatch();
 useEffect(() => {
        dispatch(getdata())
    }, []);


    return( 
  <Form>
           <FormGroup>
               <InputGroup>
                    <Input 
                    type='text'
                    name='todo'
                    id='todo'
                    placeholder='Your next Todo'
                    value={title}
                    onChange={e => setTitle(e.target.value)}
                    />
                    <InputGroupAddon addonType='prepend'>
                        <Button color='primary' onClick={()=>{
                            if(title === ''){
                                return alert('please add a todo')
                            }
                            const todo = {
                                title,
                                id:v4(),
                            }
                    
                            addTodo(todo);
                    
                            setTitle('');
                        }}>
                        
                            ADD
                        </Button>
                    </InputGroupAddon>
               </InputGroup>
           </FormGroup>
       </Form>      
    )
}

const mapStateToProps = state => ({

}) 

const mapDispatchToProps = dispatch =>({
    addTodo : todo =>{
        dispatch(addTodo(todo))
    },
})


export default connect(mapStateToProps,mapDispatchToProps)(TodoForm)

getData(todo) - action creator function require argument getData(todo) - 动作创建者 function 需要参数

export const getdata = (todo) => {
    return (dispatch) => {
        axios.post(`${API}addtodo`)
        .then(res => {
            console.log(res)
            dispatch({
                type : 'FETCH_TODO',
                payload : todo
            })
        })
        .catch(err =>{
            console.log(err);
        })
    }
}

and you call it without argument你没有争论地称呼它

const dispatch = useDispatch();
 useEffect(() => {
        dispatch(getdata())
    }, []);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 redux 加载资源失败:服务器响应状态为 400(错误请求) - redux Failed to load resource: the server responded with a status of 400 (Bad Request) Axios post 请求失败,状态码为 400 - Axios post request failed with status code 400 UnhandledPromiseRejectionWarning:错误:请求失败,状态码为 400 - UnhandledPromiseRejectionWarning: Error: Request failed with status code 400 Laravel 7 - 加载资源失败:服务器响应状态为 400(错误请求) - Laravel 7 - Failed to load resource: the server responded with a status of 400 (Bad Request) 反应:未捕获(承诺)错误:请求失败,状态码为 400 - React : Uncaught (in promise) Error: Request failed with status code 400 Steam Post请求-加载资源失败:服务器响应状态为400(错误请求) - Steam Post Request - Failed to load resource: the server responded with a status of 400 (Bad Request) AJAX调用错误 - 状态为400(错误请求) - AJAX call error - status of 400 (Bad Request) Express BodyParser响应错误请求,状态400? - Express BodyParser responds with Bad Request, Status 400? 非常简单的AngularJS $ http POST结果为'400(错误请求)'和'无效的HTTP状态代码400' - Very Simple AngularJS $http POST Results in '400 (Bad Request)' and 'Invalid HTTP status code 400' 无法加载资源:服务器响应状态为 400(错误请求)Spring JS 对控制器的调用不起作用 - Failed to load resource: the server responded with a status of 400 (Bad Request) Spring JS call to Controller not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM