【发布时间】:2021-03-24 21:57:29
【问题描述】:
您好,我正在尝试使用 Firebase 函数读取存储在 Firestore 中的用户文档。每个用户都有一个独特的文档,其中包含无法存储在 Firebase Auth 上的额外数据。文档名称是用户 UID。 但是当我试图在我的可调用函数上阅读它时,我无法访问它。
创建用户时创建文档的代码:
exports.createdacc = functions.auth.user().onCreate(user => {
console.log('User created', user.phoneNumber);
return admin.firestore().collection('users').doc(user.uid).set({
number: user.phoneNumber,
verified: false,
});
});
读取该文档的可调用函数,以便我做出一些决定
exports.checkVerification = functions.https.onCall((data, context) => {
if (!context.auth){
throw new functions.https.HttpsError('unauthenticated');
}
console.log('user is ', context.auth.uid);
const user = admin.firestore().collection('users').doc(context.auth.uid);
user.get().then(doc => {
//temp code -- Not working
console.log('data read');
if (doc.get().verified){
console.log('verified');
} else {
console.log('not verified');
}
return "success";
}).catch(error => {
throw new functions.https.HttpsError('internal');
});
});
为什么我看不懂文档?里面什么都没有执行。
【问题讨论】:
-
您是否在firebase控制台中检查过文档是否已创建
-
@development-ninja 是的,文档存在。我什至可以使用库从 android 访问它。
-
好的,你在可调用函数中得到了正确的 context.auth.uid 吗?
-
@development-ninja 是的,uid 匹配。
标签: node.js firebase google-cloud-firestore google-cloud-functions