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

时间:2020-07-01 05:01:06

标签: javascript reactjs react-redux react-hooks redux-thunk

我收到一个错误“ 400错误的请求”,我找不到我的错误是什么 下面我分享我的前端和后端代码。 我也分享我来的图片错误链接 https://ibb.co/swQPgYG

@@@@@@@@@@@@@@@@@@@@@后端@@@@@@@@@@@@@@@@@@@@@@@@

todoschema.js

这是一个待办事项

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

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

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

module.exports = router;

auth.js / controller

 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 ##################### / p>

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

在这里我从后端获取请求

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);
        })
    }
}

这是我在其中添加待办事项的todoForm

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)

1 个答案:

答案 0 :(得分:0)

getData(todo) - 动作创建器函数需要参数

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);
        })
    }
}

你不加论证地调用它

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