簡體   English   中英

使用 Firebase REST API 時如何處理注冊用戶錯誤

[英]How to handle sign up a user error while using Firebase REST API

我一直在嘗試使用 Node & Axios HTTP 庫使用 Firebase Rest API 注冊用戶。

我可以使用emailpassword創建一個新用戶。

但是當數據庫中email already existspassword is less than 6 characters ,它不會顯示正確的錯誤。

它只返回Request failed with status code 400

這是我的代碼。

const config = 
    {
        headers:
        {
            'Content-Type': 'application/json',
        },
    }; 


    data = {
        'email': "test@test.com",
        'password': "password123",
        'returnSecureToken': true
    }

    try
    {
        let signup = await axios.post('https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=API_KEY',data,config)

        res.json(signup.data);
    }
    catch(err)
    {
        res.json(err);
    }

我得到的錯誤。

{
    "message": "Request failed with status code 400",
    "name": "Error",
    "stack": "Error: Request failed with status code 400\n    at createError (/Users/user/NodeJS/FirebaseAuth/node_modules/axios/lib/core/createError.js:16:15)\n    at settle (/Users/user/NodeJS/FirebaseAuth/node_modules/axios/lib/core/settle.js:17:12)\n    at IncomingMessage.handleStreamEnd (/Users/user/NodeJS/FirebaseAuth/node_modules/axios/lib/adapters/http.js:244:11)\n    at IncomingMessage.emit (events.js:327:22)\n    at endReadableNT (_stream_readable.js:1224:12)\n    at processTicksAndRejections (internal/process/task_queues.js:84:21)",
    "config": {
        "url": "https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=API_KEY",
        "method": "post",
        "data": "{\"email\":\"test@test.com\",\"password\":\"password123\",\"returnSecureToken\":true}",
        "headers": {
            "Accept": "application/json, text/plain, */*",
            "Content-Type": "application/json",
            "User-Agent": "axios/0.21.0",
            "Content-Length": 75
        },
        "transformRequest": [
            null
        ],
        "transformResponse": [
            null
        ],
        "timeout": 0,
        "xsrfCookieName": "XSRF-TOKEN",
        "xsrfHeaderName": "X-XSRF-TOKEN",
        "maxContentLength": -1,
        "maxBodyLength": -1
    }
}

這里的預期錯誤是:電子郵件已經存在。 但它不會返回此錯誤。

這可能是什么問題?

謝謝!

默認情況下,firebase 不會在錯誤正文中返回響應,但可以使用它進行提取。

console.log(err.response.data.error);

官方 firebase 文檔中沒有提到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM