问题是,当我在 JavaScript 中使用 error.code 时,当我希望 error.code 吐出有意义的 Supabase Auth 错误代码时,我得到“未定义”。
我尝试了 error.message、error.name 和 error.status,它们都给了我有意义的结果。值得注意的是 error.code 存在,尽管它只是返回“undefined”。
这里是一些代码和输出,用于进一步诊断问题并查看错误对象的所有可用属性:
代码:
async function signIn() {
const email = document.querySelector('.sign-in-popup-sign-in-email-input').value;
const password = document.querySelector('.sign-in-popup-sign-in-password-input').value;
const { user, session, error } = await supabaseClient.auth.signInWithPassword({
email: email,
password: password
});
if (error && error.message === 'Invalid login credentials') {
console.error(error);
// Get all property names of the error object
let properties = Object.getOwnPropertyNames(error);
// Create an array to store property-value pairs
let propertyStrings = [];
// Iterate over each property and build the output string
properties.forEach(function (prop) {
console.log(prop, error[prop]); // Log each property and its value
propertyStrings.push(prop + ': ' + error[prop]);
});
// Join the property strings with a newline for readability
let alertMessage = propertyStrings.join('\n');
// Alert all properties
alert(alertMessage);
async function signUp() {
const { user, error } = await supabaseClient.auth.signUp({
email: email,
password: password
});
}
signUp();
}
}
输出:
message: Invalid login credentials
__isAuthError: true
name: AuthApiError
status: 400
code: undefined
line: 7
column: 4034
sourceURL: https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2
stack: w@https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2:7:4034
T@https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2:7:4221
@https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2:7:6534
在未输入电子邮件或密码的情况下单击“登录”后,控制台日志中的一些代码:
[Error] Failed to load resource: the server responded with a status of 400 () (token, line 0)
[Error] AuthApiError: Invalid login credentials
(anonymous function) (index.html:68)
这是 Supabase 身份验证错误代码的一个已知问题。它正在 Supabase Auth GitHub 存储库上进行跟踪https://github.com/supabase/auth/issues/1631