加载资源失败:使用firebase云功能访问云firestore时,服务器响应状态为500() &g



我知道有类似的帖子,但我试了所有的,没有工作。我编写这个函数是为了从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.');
}
})

当它部署时一切正常,但当我调用它时,我得到这两个错误,我不知道如何解决它。

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."
}
} 

解决了我的问题。这是因为我使用的是云Firestore而不是实时数据库,因为很多例子和文档都在使用它,我认为它只是有不同的语法🤦🏻下面是固定的代码:

exports.getDisplayName = functions.https.onCall((data, context) => {
if(context.auth){
const uid = data.uid
return admin.firestore().collection("users").doc(uid).get().then(doc=>{
console.log(doc)
return{name: doc.data().displayName}
})
}else{
throw new functions.https.HttpsError('failed-precondition', 'The function must be called while authenticated.');
}
})

admin.firestore()而不是admin.database()

相关内容

  • 没有找到相关文章

最新更新