【发布时间】:2019-05-09 21:10:43
【问题描述】:
我只是想使用 firebase 函数获取经过身份验证的用户的“uid”。但每次我在“firebase functions:shell”中运行该函数时。我得到这些错误。谁能帮我解决这个问题?
“context.auth.uid”TypeError:无法读取未定义的属性“uid”
exports.sendNewUser = functions.database.ref('/Users/')
.onCreate(async (snapshot, context) => {
const userId = await context.auth.uid;
const tokenPath = await admin.database().ref('Notify').once('value')
var message = []
tokenPath.forEach((childSnapshot) => {
var allTokens = childSnapshot.val().expo
if (allTokens) {
message.push({
"to": allTokens,
"header": " Users",
"body": "New user has been added, check them out!!!"
})
}
})
await fetch('https://exp.host/--/api/v2/push/send', {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(message)
})
console.log("The user ID is : " + userId)
})
包.json:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"dateformat": "^3.0.3",
"firebase-admin": "6.3.0",
"firebase-functions": "^2.1.0",
"node-fetch": "^2.3.0"
},
"devDependencies": {
"eslint": "^4.12.0",
"eslint-plugin-promise": "^3.6.0"
},
"private": true
}
【问题讨论】:
-
您能分享一下您正在使用的 Firebase Functions 版本吗?你会在 package.json 文件中找到它。
-
是的!我刚刚添加了它:)
-
好的,你的版本看起来不错,即它符合你的参数。其他问题:您是否使用 auth 选项以特定最终用户身份运行您的功能,请参阅firebase.google.com/docs/functions/…
-
另外,请注意,如果我没记错的话,您不应该这样做
const userId = await context.auth.uid;而只是const userId = context.auth.uid; -
是的,我浏览了文档!!!需要将身份验证添加到函数中,一旦我将其部署到云中,它会获取特定的登录用户
uid吗?!
标签: firebase react-native firebase-authentication google-cloud-functions