[我正在尝试使用Firebase Cloudfunctions和带有nodeJS的Admin SDK创建用户时遇到错误。
[创建用户时,一切正常,因为创建的用户返回了我,但是创建错误时,我没有得到应允的错误。
因此,在前端方面,什么也不会收到任何东西,就好像它是正确的一样
示例:我没有输入电子邮件来创建新用户,并且请求的状态为200,但答案是:
{“ result”:{“ errorInfo”:{“ code”:“ auth / invalid-password”,“ message”:“密码必须是至少包含6个字符的字符串。”},“ codePrefix” :“ auth”}}
这是我的功能代码:
exports.addNewUser = functions.https.onCall((data, context) => {
return admin.auth().createUser({
email: data.email,
emailVerified: true,
password: data.password,
displayName: data.name,
disabled: false
}).then(userRecord => {
console.log('Successfully created new user:', userRecord.uid);
return userRecord;
})
.catch( error => {
console.log('Error creating new user:', error);
return error;
});
});
这在创建用户时很好用,但如果存在错误,则错误不会再次出现。
我所做的是,不是返回错误,而是仅返回一个字符串,并且如果该字符串正确返回,则该字符串。
这是因为要处理可调用云函数中的错误,您需要抛出functions.https.HttpsError
,如functions.https.HttpsError
中所述。
因此,以下将起作用:
documentation
还要注意文档中有关如何处理客户端错误的exports.addNewUser = functions.https.onCall((data, context) => {
return admin.auth().createUser({
email: data.email,
emailVerified: true,
password: data.password,
displayName: "dataname",
disabled: false
}).then(userRecord => {
console.log('Successfully created new user:', userRecord.uid);
return userRecord;
})
.catch(error => {
console.log('Error creating new user:', error);
throw new functions.https.HttpsError('invalid-argument', error.message);
});
});
。