【问题标题】:React native axios calling action inside .then callback not working在.then 回调中反应本机 axios 调用操作不起作用
【发布时间】:2018-02-10 11:54:47
【问题描述】:

我在 axios 发布请求 .then 方法中调用 axios 时遇到问题。

收到以下错误消息:

SyntaxError ... Unexpected token, expected , (145:82) (null)

第 145 行是调用 axios 后的 .then 回调。

这是我的代码:

_handleImagePicked = async pickerResult => {
    this.setState({uploading: true})
    let uploadResponse, uploadResult;

    try {
      this.setState({ uploading: true });

      if (!pickerResult.cancelled) {
        uploadResponse = await uploadImageAsync(pickerResult.uri);
        uploadResult = await uploadResponse.json();
        this.setState({ image: uploadResult.location });
        axios.post( `https://f1cdfd5fa4e.ngrok.io/api/update_photo?email=${this.props.email}&image=${uploadResult.location}`)
          .then(response => { this.props.profileUpdate({ prop: 'photo', response.data }) })
          .catch(error => console.log(error))

      }
    } catch (e) {
      console.log({ uploadResponse });
      console.log({ uploadResult });
      console.log({ e });
      alert('Upload failed, sorry :(');
    } finally {
      this.setState({ uploading: false });
    }
  };

【问题讨论】:

    标签: reactjs react-native redux axios


    【解决方案1】:

    您将 { prop: 'photo', response.data } 作为参数传递给回调 但 response.data 不能作为键添加 尝试发送

    this.props.profileUpdate({prop: 'photo', data:response.data})

    【讨论】:

    • 对我来说太棒了,它是价值:...不是数据:..但它有效,谢谢!我很好奇,因为我一直在我的应用程序的其余部分使用这种语法,而没有指定“数据:”并且它工作正常,为什么在这种情况下它不起作用?
    • 例如:var a = 1; var b = {a} 被评估为 var b = { a : a}
    猜你喜欢
    • 2021-01-22
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 2016-10-22
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    相关资源
    最近更新 更多