在Firebase云功能中获取用户uid

时间:2017-11-22 22:46:32

标签: firebase firebase-realtime-database google-cloud-functions

下面是我的数据库节点的JSON:

{
  "1YrpX2W2xnMPoy4YGpZcOE0xJ5g2" : {
    "email" : "muyi@hotmail.com",
    "fullname" : "Muyiz",
    "selection" : [ 1, 2, 3, 4, 5, 6 ],
    "teamname" : "awon",
    "total" : 12,
    "userName" : "motmail.com",
    "week1" : 0,
    "week10" : 0,
    "week11" : 0,
    "week12" : 0,
    "week2" : 0,
    "week3" : 0,
    "week4" : 0,
    "week5" : 0,
    "week6" : 12,
    "week7" : 0,
    "week8" : 0,
    "week9" : 0
  },
  "6K9rQiZQ3jaV38WWtDbNwxhqIwc2" : {
    "email" : "domi@gmail.com",
    "fullname" : "Dai",
    "selection" : 0,
    "teamname" : "Bayern Neverlosin'",
    "total" : 0,
    "userName" : "dai@gmail.com",
    "week1" : 0,
    "week10" : 0,
    "week11" : 0,
    "week12" : 0,
    "week2" : 0,
    "week3" : 0,
    "week4" : 0,
    "week5" : 0,
    "week6" : 0,
    "week7" : 0,
    "week8" : 0,
    "week9" : 0
  },
  "9OgN4HyMtARaQEQV1mKQ5lyE1992" : {
    "email" : "jonail.com",
    "fullname" : "Jon",
    "selection" : [ 40, 8, 10, 24, 18, 34 ],
    "teamname" : "Chad fc",
    "total" : 0,
    "userName" : "jojin@gmail.com",
    "week1" : 0,
    "week10" : 0,
    "week11" : 0,
    "week12" : 0,
    "week2" : 0,
    "week3" : 0,
    "week4" : 0,
    "week5" : 8,
    "week6" : 0,
    "week7" : 0,
    "week8" : 0,
    "week9" : 0
  },
  "AGVZAUye5ZbZgvwCOpMeDkoOsEU2" : {
    "email" : "ihe@gmail.com",
    "fullname" : "Emeka Iheme",
    "selection" : 0,
    "teamname" : "Young Money",
    "total" : 0,
    "userName" : "ihem@gmail.com",
    "week1" : 0,
    "week10" : 0,
    "week11" : 0,
    "week12" : 0,
    "week2" : 0,
    "week3" : 0,
    "week4" : 0,
    "week5" : 0,
    "week6" : 0,
    "week7" : 0,
    "week8" : 0,
    "week9" : 0
  }
}

有没有办法在用户节点下访问每个用户的用户uid?

目标是在我的云功能中运行以下代码:

ref = admin.database().ref(`/users/${user.uid}/week1`);

但就目前而言,以下云函数会返回未定义用户的错误:

exports.update = functions.database.ref('/users/{uid}')
    .onWrite(event=>{
ref = admin.database().ref(`/users/${user.uid}/week1`);
return ref.set(50);

}

1 个答案:

答案 0 :(得分:0)

在数据库触发器中定义通配符时:

functions.database.ref('/users/{uid}')

uid通配符可以作为event.params.uid从传递的事件中访问。有关详细信息,请参阅documentation