【问题标题】:Unable to make API call from react native app in local machine无法从本地机器中的本机应用程序进行 API 调用
【发布时间】:2021-08-23 19:10:23
【问题描述】:

我正在我的 Mac 上的 android 模拟器上运行我的应用程序,并尝试访问部署在 firebase 上的服务端点。我收到 500 错误,什么也没说,当我尝试打印错误时,它显示 There was a problem sending log messages to your development environment [PrettyFormatPluginError: value.hasOwnProperty is not a function. (In 'value.hasOwnProperty('tag')', 'value.hasOwnProperty' is undefined)]

当我尝试使用邮递员和相同的有效负载访问同一个端点时,我能够成功地做到这一点。

以下代码写在应用程序中

fetch('https://{app_url}.cloudfunctions.net/app/user', {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json'
     },
     body: userPayload
})
.then(response => response.text())
.then(result => {
     console.log('User in DB created');
     console.log(result);
})
.catch(error => console.log('error', error));

有人可以帮我吗?

【问题讨论】:

  • 只是猜测 userPayload 可能为空或缺少您的后端期望的标签信息,您可能正在测试。
  • 您好@Atmas,我正在从我的应用程序传递相同的有效负载,并且不需要标签信息。但这无济于事。
  • 你能粘贴你的用户负载吗?
  • “app_url”呢?您确定它指向正确的网址吗?
  • 有效载荷:{“dob”:“09/09/1996”,“email”:“test1@gmail.com”,“nameOfTheUser”:“ABtest”,“phoneNumber”:“9898989898” ", "userId": "49178" }

标签: firebase react-native fetch


【解决方案1】:

您正在设置 Accept: 'application/json',因此您应该将“.then(response => response.text())”更改为“.then(response => response.json())”。让我知道它是否有效。

fetch('https://{app_url}.cloudfunctions.net/app/user', {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json'
     },
     body: userPayload
})
.then(response => response.json())
.then(result => {
     console.log('User in DB created');
     console.log(result);
})
.catch(error => console.log('error', error));

【讨论】:

  • 你好,它没有帮助。
  • 好主意.. 但如果他遇到 500 错误,它会立即落入 .catch 中,甚至不会到达这一行。
猜你喜欢
  • 2018-08-21
  • 2023-04-09
  • 2022-01-05
  • 2016-09-26
  • 2018-12-29
  • 2015-03-29
  • 2021-10-27
  • 2016-08-31
  • 2012-06-14
相关资源
最近更新 更多