【问题标题】:Using React and trying to post data with axios- coming up empty object使用 React 并尝试使用 axios 发布数据 - 出现空对象
【发布时间】:2020-03-24 17:00:33
【问题描述】:

新的反应。一般来说,编码很新。我只是想将drinkInfo 中的数据发布到我的数据库中。

export default function CardEdit(props) {
  const classes = useStyles();

const addDrinkToDatabase = () => {

  console.log("props.reduxState is" + props.reduxState)

  let drinkInfo = {
    type: props.reduxState.type,
    name:  props.reduxState.name,
    manufacturer: props.reduxState.manufacturer,
    rating: props.reduxState.rating,
    date: props.reduxState.date,
    description: props.reduxState.description,
    favorite: props.reduxState.favorite
  }

console.log("info is" + drinkInfo)

var options = {
  method: 'POST',
  url: 'myurlishere',
  data: drinkInfo
};

axios.request(options)
  .then(response => {
    console.log("My response", response);
  })
  .catch(error => {
    console.log("MyError:", error);
  })
}

  return (
    <div>
      <button onClick = { addDrinkToDatabase } >Log Drink</button>
    </div>
)

我知道数据会正确返回,但我的 console.log 会返回“props.reduxState is [object Object]”。我确定我忽略了一些简单的事情。

【问题讨论】:

    标签: reactjs post axios


    【解决方案1】:

    将您的 console.log 更改为

    console.log("props.reduxState is", props.reduxState)
    

    在 javascript 中连接字符串和对象会产生[object Object]

    【讨论】:

      猜你喜欢
      • 2021-05-26
      • 2019-06-18
      • 2022-01-18
      • 1970-01-01
      • 2020-11-26
      • 2023-02-04
      • 2017-09-14
      • 1970-01-01
      • 2022-11-19
      相关资源
      最近更新 更多