【问题标题】:Firebase REST API doesn't respond with common error codes using Fetch API on React NativeFirebase REST API 不使用 React Native 上的 Fetch API 响应常见错误代码
【发布时间】:2022-01-21 14:52:21
【问题描述】:

Firebase API doesn't respond with common error codes using Axios on react native 这里也有类似的讨论,但那家伙正在使用 Axios。我想使用 Fetch API,并希望在当前情况下使用此 API 有类似的解决方案。

问题是当我从 Firebase REST API 收到错误时,响应的状态文本为空。我只有状态码。

如果我将使用 Axios 使用相同的 url 和选项发出相同的请求,我将使用 console.log(error.response.data.error.message) 得到 Firebase API 违反的错误描述(如 TOKEN_EXPIRED、USER_DISABLED、USER_NOT_FOUND、INVALID_REFRESH_TOKEN、MISSING_REFRESH_TOKEN 等)

如何使用 Fetch API 实现相同的输出?

 const response = await fetch(url, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: bodyString
    });

if (response.ok) {
    const payload = await response.json();
    return payload as HttpResponse;
} else {
    throw new Error(`Server error: ${response.statusText}. Status: ${response.status}`);
}

【问题讨论】:

标签: javascript firebase rest fetch


【解决方案1】:

我不知道我是怎么错过检查这个的。 解决办法:await response.text()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    相关资源
    最近更新 更多