我有一个Google函数,我从我的RN应用程序中调用它,但它正在返回
错误:内部
我已经将权限设置为未通过身份验证的用户,因此任何人都可以调用它--仅用于测试目的。当我设置为经过身份验证的用户权限时,它会引发另一个错误:即使我是经过身份验证的,并且可以在我的应用程序中获得currentUser id,但它仍然会引发另一个错误。
尝试搜索这个错误,但它没有送我到任何可能的解决方案,所以决定在这里张贴,并希望收到的答复,将帮助我解决它。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.createUser = functions.region('europe-west1').https.onCall(async (data, context) => {
try {
//Checking that the user calling the Cloud Function is authenticated
if (!context.auth) {
throw new UnauthenticatedError('The user is not authenticated. Only authenticated Admin users can create new users.');
}
const newUser = {
email: data.email,
emailVerified: false,
password: data.password,
disabled: false
}
const role = data.role;
const userRecord = await admin
.auth()
.createUser(newUser);
const userId = userRecord.uid;
const claims = {};
claims[role] = true;
await admin.auth().setCustomUserClaims(userId, claims);
return { result: 'The new user has been successfully created.' };
} catch (error) {
if (error.type === 'UnauthenticatedError') {
throw new functions.https.HttpsError('unauthenticated', error.message);
} else if (error.type === 'NotAnAdminError' || error.type === 'InvalidRoleError') {
throw new functions.https.HttpsError('failed-precondition', error.message);
} else {
throw new functions.https.HttpsError('internal', error.message);
}
}
});在我的RN应用程序中,我这样称呼它:
var user = {
role: role
}
const defaultApp = firebase.app();
const functionsForRegion = defaultApp.functions('europe-west1');
const createUser = await functionsForRegion.httpsCallable('createUser');
createUser(user)
.then((resp) => {
//Display success
});
console.log(resp.data.result);
})
.catch((error) => {
console.log("Error on register patient: ", error)
});我认为我在RN应用程序中调用它的方式是正确的,因为我已经用testFunction测试了它,并返回了一个简单的字符串。所以,我认为问题就在函数本身的某个地方。
编辑:,我只是通过简单地调用函数并返回上下文来测试,它总是返回内部错误:
exports.registerNewPatient = functions.region('europe-west3').https.onCall((data, context) => {
return context; //this is returned as INTERNAL error.
}我只是无法理解这里发生了什么,为什么当我作为一个用户进行身份验证并且应该返回经过身份验证的用户数据时,它会返回内部错误,对吗?
发布于 2020-04-23 00:27:43
在您的console.log(context) ; console.log(data)函数中尝试一些registerNewPatient语句,并查看日志。他们怎么说?
需要考虑的其他事情可能包括在客户端代码中使用europe-west1,而函数代码有europe-west3。试着把这些排成一列,看看它是否有效?根据我的经验,如果不存在指定的函数,客户端将收到一个内部错误。
https://stackoverflow.com/questions/61374792
复制相似问题