首页 > 解决方案 > “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
}

标签: firebasereact-nativefirebase-authenticationgoogle-cloud-functions

解决方案


context.auth.uid 将在 onCreate 触发器中未定义,因为这是由云函数运行时触发的,而不是由创建文档的用户触发的。

https://stackoverflow.com/a/64345666/4868790


推荐阅读