首页 > 解决方案 > 无法从本地机器中的本机应用程序进行 API 调用

问题描述

我在我的 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));

有人能帮我一下吗 ?

标签: firebasereact-nativefetch

解决方案


您正在设置 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));

推荐阅读