简体   繁体   中英

Axios.post in React 422, Error: (Data Validation Failed.)

I have an item object that wants to send to the array by the post method. I receives the error: POST https://applic.com/api/v1/todos?expand=createdBy 422 (Data Validation Failed.)

my item object:

creat: Sat Jun 01 2019 00:15:00 GMT+0200 (Central European summer time) {}
desc: "bbbb"
id: "123456-9bbc-4f85-1234-fdfdfdfdfdfdds"
price: 900
stat: "50"
parent_id: "12345678-123r-45frt6-b6678-12345567"

example object in api:

creat: "2019-06-28 12:58:02+00"
desc: null
id: "c123545-12sd-67ui-w234-5ghg789"
pend: false
price: 60
stat: 50
parent_id: "12345678-123r-45frt6-b6678-12345567"
updat_by: null

I correct it: my object --> stat: parseInt(50) property creat --> toISOString --> return me "2019-06-29T12:45:53.594Z"

Api return me "2019-06-28 12:58:02+00"

Is it problem 2019-06-29T12:45:53 .594Z

2019-06-28 12:58:02 +00 ?

I have to send as request body

Code:

const url = `https://applic.com/api/v1/todos?expand=createdBy`;
const token = '12345'; 


add = (item) => {
    axios.post(
        url,
        {
            data: item
        },
        { 
            headers: { 'Authorization' : `Bearer ${token}`}
        }).then( res => {
            console.log(res.data);
        }).catch( err => {
            console.log(err);
        });

      let newArray = [...this.state.todos];

      newArray.push(item);

      this.setState({
        todos: newArray
      });
}

How about this ?

const url = `https://applic.com/api/v1/todos?expand=createdBy`;
const token = '12345';


add = (item) => {

  axios({
    method: 'POST',
    url,
    data: item,
    headers: { Authorization: `Bearer ${token}` },
  })
  .then(res => {
    console.log(res.data);
  }).catch(err => {
    console.log(err);
  });

  this.setState({
    todos: [...this.state.todos, item],
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM