【发布时间】:2021-02-28 21:14:24
【问题描述】:
我知道有关于此的类似帖子,但我尝试了所有这些并且没有工作。
我编写了这个函数来从 UID 中获取用户名。我有一个名为 users 的集合,每个用户的 uid 作为文档的名称,其中有一个名为 displayName 的值。
exports.getDisplayName = functions.https.onCall((data, context) => {
if(context.auth){
const uid = data.uid
return admin.database().ref(`/users/${uid}`).get().then(doc=>{return{name: doc.data().displayName}})
}else{
throw new functions.https.HttpsError('failed-precondition', 'The function must be called while authenticated.');
}
})
当它部署它时,一切正常,但当我调用它时,我得到这 2 个错误,我不知道如何解决它。
Failed to load resource: the server responded with a status of 500 ()
Unhandled Promise Rejection: Error: INTERNAL
我还有另一个可以正常工作的功能:
exports.getUidFromEmail = functions.https.onCall((data, context) => {
if(context.auth){
const email = data.email
return admin.auth().getUserByEmail(email).then((userRecord)=>{ return {user: userRecord.uid}})
}else{
throw new functions.https.HttpsError('failed-precondition', 'The function must be called while authenticated.');
}
})
编辑:它说的错误是这样的:
Unhandled error FirebaseError: Can't determine Firebase Database URL.
at FirebaseDatabaseError.FirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:44:28)
at new FirebaseDatabaseError (/workspace/node_modules/firebase-admin/lib/utils/error.js:205:23)
at DatabaseService.ensureUrl (/workspace/node_modules/firebase-admin/lib/database/database-internal.js:97:15)
at DatabaseService.getDatabase (/workspace/node_modules/firebase-admin/lib/database/database-internal.js:65:26)
at FirebaseApp.database (/workspace/node_modules/firebase-admin/lib/firebase-app.js:228:24)
at FirebaseNamespace.fn (/workspace/node_modules/firebase-admin/lib/firebase-namespace.js:191:45)
at /workspace/index.js:17:22
at func (/workspace/node_modules/firebase-functions/lib/providers/https.js:273:32)
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
errorInfo: {
code: 'database/invalid-argument',
message: "Can't determine Firebase Database URL."
}
}
【问题讨论】:
-
uid 通常是 CONTEXT 的一部分 - 我在猜测(因为您没有显示它)您的“工作正常”是使用具有电子邮件字段的对象的有效负载调用的。您的 FIRST 函数还尝试从有效负载中获取 uid - 但我打赌您打算从 context.auth 中获取它...
-
@LeadDreamer 所以我不是试图获取调用该函数的用户的 displayName,而是任何用户的名称,您可以传入该用户的 uid。 (这个功能的原因是有些地方只是通过uid存储用户,所以我需要得到他们的名字,这样人们才能真正知道那个人是谁)
-
嗨,也许你可以 check the logs 为你的 Firebase 功能。在那里您可以查看完整的回溯,这可能有助于调试您的代码。
-
@llompalles 好的,我检查了日志并对我的帖子进行了编辑
标签: firebase google-cloud-firestore google-cloud-functions