簡體   English   中英

可能未處理 Promise 拒絕 (id: 0): ReferenceError: 錯誤未定義

[英]Possible Unhandled Promise Rejection (id: 0): ReferenceError: error is not defined

//LoginScreen.js

import signIn from "amplify-communication"
    
  const LoginScreen = props => {
    function _signIn(username, password){
        const request = signIn(username, password);
        if(request.isCommited)
           console.log("User is Signed In");
        else
           console.log("Error Signing In", request.message);
        
       }
    }

// amplify-communication.js
import { Auth } from "aws-amplify";
export async function signIn(_username, _password) {
    try {
        var user = await Auth.signIn(_username, _password);
    } catch (error) {
        console.log('Error Signing in');
    }
    finally {
        return {
            isCommitted: error ? false : true,
            message: error,
            payload: user
        }
    }
}

我有單獨的文件用於與 Amplify 通信。 我要返回一個帶有不同密鑰的 object。 在運行代碼時,它會向我顯示以下警告。

1 - 可能未處理 Promise 拒絕 (id: 0): ReferenceError: error is not defined ReferenceError: error is not defined

2- 使用不安全的隨機數生成器,只有在不支持 crypto.getRandomValues 的調試器中運行時才會發生這種情況

error的范圍僅限於catch塊,因此當您嘗試在finally塊中使用error時,它是一個未聲明的標識符。 您在成功路徑上遇到與user相同的問題。

相反,要么在變量中跟蹤該信息,要么重復return語句。

export async function signIn(_username, _password) {
    try {
        const user = await Auth.signIn(_username, _password);
        return {
            isCommitted: true,
            payload: user,
            message: undefined, // if you need it, otherwise remove
        };
    } catch (error) {
        console.log('Error Signing in');
        return {
            iscCommitted: false,
            payload: undefined, // if you need it, otherwise remove
            message: error,
        };
    }
}

關於隨機數生成器的事情聽起來像是您正在使用的關於運行代碼的環境的 package 的警告(例如,它沒有強大的隨機數生成器)。


旁注:將錯誤轉換為這樣的返回值可能是正確的,但更多時候允許錯誤傳播到調用者(可能傳播到它的調用者等)而不是強制每個級別都更正確檢查返回值以查看是否有效。 同樣,在不知道這個 function 是做什么用的情況下,這可能適用於也可能不適用於此處,但只是將其標記出來。

暫無
暫無

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

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