React 422中的Axios.post,错误:(数据验证失败。)

时间:2019-06-28 13:22:43

标签: javascript reactjs axios

我有一个item对象,希望通过post方法发送到数组。我收到错误: POST https://applic.com/api/v1/todos?expand=createdBy 422 (Data Validation Failed.)

我的物品对象:

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"

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

我更正了: 我的对象-> stat: parseInt(50) 属性creat-> toISOString->还给我"2019-06-29T12:45:53.594Z"

Api还给我"2019-06-28 12:58:02+00"

问题2019-06-29T12:45:53 .594Z

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

我必须作为请求正文发送

代码:

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

1 个答案:

答案 0 :(得分:2)

这个怎么样?

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