firebase - “context.auth.uid”类型错误:无法读取未定义的属性“uid”
问题描述
我只是想使用 firebase 函数获取经过身份验证的用户的“ uid ”。但是每次我在“ firebase functions:shell ”中运行该函数时。我得到这些错误。谁能帮我解决这个问题?
“context.auth.uid”类型错误:无法读取未定义的属性“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
}
解决方案
context.auth.uid 将在 onCreate 触发器中未定义,因为这是由云函数运行时触发的,而不是由创建文档的用户触发的。
推荐阅读
- facebook - Facebook Ads API - 实时获取广告账户余额
- automated-tests - 自动化测试 - Katalon 工作室
- python - 是否有任何与 haskell 的 "it" 等效的 python 来获取 REPL 中的最后一个成功命令
- javascript - 为什么当用户关闭 chrome 中的选项卡时显示弹出窗口?
- javascript - 在 WebAssembly 中写入文件
- php - 严重未捕获的错误:在 Woocommerce 中调用 null 上的成员函数 is_on_sale()
- c# - 将组合框添加到数据网格中的单元格
- extjs - 检测何时将新行添加到网格 [EXTJS]
- java - 类必须要么声明为抽象......!错误
- mysql - mysql如何为2个不同的表制作评论表