[英]Trying to use GoogleSignIn in React Native then Possible Unhandled Promise Rejection (id: Error: DEVELOPER_ERROR
[英]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.