Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 从Firebase的云函数获取当前用户id(PubSub触发器)_Node.js_Firebase_Google Cloud Functions_Google Cloud Pubsub - Fatal编程技术网

Node.js 从Firebase的云函数获取当前用户id(PubSub触发器)

Node.js 从Firebase的云函数获取当前用户id(PubSub触发器),node.js,firebase,google-cloud-functions,google-cloud-pubsub,Node.js,Firebase,Google Cloud Functions,Google Cloud Pubsub,当我在客户端需要当前用户id时,我可以使用:firebase.auth().currentUser.uid 如何在云函数(发布子触发器)中获取当前用户id 您可以从firebase函数中的以下context.auth.id获取用户id const query = await db.collection("posts") .where("date", "==", date) .where("idUser&

当我在客户端需要当前用户id时,我可以使用:
firebase.auth().currentUser.uid

如何在云函数(发布子触发器)中获取当前用户id


您可以从firebase函数中的以下
context.auth.id
获取用户id

const query = await db.collection("posts")
      .where("date", "==", date)
      .where("idUser", "==", context.auth.id) //something like this
      .get();

您可以从firebase函数中的以下
context.auth.id
获取用户id

const query = await db.collection("posts")
      .where("date", "==", date)
      .where("idUser", "==", context.auth.id) //something like this
      .get();

不,在计划的云函数中无法获取任何UID。用户不会调用该函数。它只是根据您的日程安排定期调用。报告说:

此字段仅为实时数据库触发器和 可调用函数。对于未经身份验证的用户,此字段为空。 对于Firebase管理员用户和不提供用户权限的事件类型 信息,此字段不存在

本例中的上下文对象如下所示:

{
  "eventId": "eventId",
  "timestamp": "2021-05-16T09:21:00.505Z",
  "eventType": "google.pubsub.topic.publish",
  "params": {},
  "resource": {
    "name": "projects/<project-id>/topics/firebase-schedule-scheduledFunctionCrontab-us-central1",
    "service": "pubsub.googleapis.com",
    "type": "type.googleapis.com/google.pubsub.v1.PubsubMessage"
  }
}
{
“eventId”:“eventId”,
“时间戳”:“2021-05-16T09:21:00.505Z”,
“eventType”:“google.pubsub.topic.publish”,
“参数”:{},
“资源”:{
“名称”:“项目//主题/firebase-schedule-scheduledFunctionCrontab-us-central1”,
“服务”:“pubsub.googleapis.com”,
“type”:“type.googleapis.com/google.pubsub.v1.PubsubMessage”
}
}
要获取截止日期已过的所有用户,可以使用以下查询:

const snapshot = admin.firestore().collections("posts").where("data", "<=", Date.now()).get()
const userIds = snapshots.docs.map(post => post.data().idUser)
//Array of users IDs of posts with due date passed

const snapshot=admin.firestore().collections(“posts”)。其中(“data”),“否,您无法在计划的云函数中获取任何UID。该函数不是由用户调用的。它只是根据您的计划定期调用。该函数表示:

此字段仅为实时数据库触发器和 可调用函数。对于未经身份验证的用户,此字段为空。 对于Firebase管理员用户和不提供用户权限的事件类型 信息,此字段不存在

本例中的上下文对象如下所示:

{
  "eventId": "eventId",
  "timestamp": "2021-05-16T09:21:00.505Z",
  "eventType": "google.pubsub.topic.publish",
  "params": {},
  "resource": {
    "name": "projects/<project-id>/topics/firebase-schedule-scheduledFunctionCrontab-us-central1",
    "service": "pubsub.googleapis.com",
    "type": "type.googleapis.com/google.pubsub.v1.PubsubMessage"
  }
}
{
“eventId”:“eventId”,
“时间戳”:“2021-05-16T09:21:00.505Z”,
“eventType”:“google.pubsub.topic.publish”,
“参数”:{},
“资源”:{
“名称”:“项目//主题/firebase-schedule-scheduledFunctionCrontab-us-central1”,
“服务”:“pubsub.googleapis.com”,
“type”:“type.googleapis.com/google.pubsub.v1.PubsubMessage”
}
}
要获取截止日期已过的所有用户,可以使用以下查询:

const snapshot = admin.firestore().collections("posts").where("data", "<=", Date.now()).get()
const userIds = snapshots.docs.map(post => post.data().idUser)
//Array of users IDs of posts with due date passed


const snapshot=admin.firestore().collections(“posts”)。其中(“data”对于计划的云函数,这是不正确的。auth属性为null。TypeError:无法读取未定义的属性“id”。对于计划的云函数,这是不正确的。auth属性为null。TypeError:无法读取未定义的属性“id”表示需要在params right中添加uid?@DipanSharma用户无法调用计划d cloud函数,因此它在上下文中没有UID。它由发布/订阅触发器调用。签出。只有当它由rtdb触发器触发或是可调用函数时,上下文对象才会包含身份验证信息。是的,我明白你的意思,但必须有某种方法可以存储或获取UID:(@DipanSharma这取决于您尝试执行的操作。OP没有指定任何用例。是的@DharamrajIt意味着需要在参数中添加uid,对吗?@DipanSharma用户无法调用计划的云函数,因此它在上下文中没有uid。它由发布/订阅触发器调用。签出。上下文对象仅在它是由rtdb触发器触发或是一个可调用的函数。是的,我明白你的意思,但我们必须有某种方法来存储或获取uid:(@DipanSharma这取决于你想做什么。OP没有指定任何用例。是的@dharmaraj虽然dharmaraj已经回答了你的问题,但是你使用这个触发器的目的是更好地实现的,它会给你创建的帖子和谁创建的帖子。但是,我不确定你为什么要发送关于p的通知ost发送给发布它的人。我正在向发布它的人发送通知,因为他们正在发布发票。在发票的到期日,它会收到一个推送通知以记住到期日。@WafaBergaoui我猜如果用户的发票处于挂起状态,您想向用户发送通知。请共享一个屏幕截图存储到期数据的发票文档。这是屏幕截图@WafaBergaoui,我添加了一个解决方法,您可以使用,如果答案有帮助,您可以通过单击勾选图标接受它,以便其他人知道它已解决,否则请随时问进一步的问题。虽然达玛拉已经回答了您的问题,但您使用的是什么使用将向您提供创建的帖子和创建它的人的帖子,可以更好地实现ng此触发器。但是,我不确定您为什么要向发布帖子的人发送关于帖子的通知。我向发布帖子的人发送通知,因为他们正在发布发票。在发票到期日,它将接收推送通知以记住到期日期。@WafaBergaoui我猜如果用户的发票处于挂起状态,您希望向用户发送通知。请共享存储到期数据的发票的Firestore文档的屏幕截图。这是屏幕截图@WafaBergaoui,我添加了一个解决方法,如果答案为如果有帮助,您可以通过单击勾选图标来接受它,以便其他人知道它已解决,否则请随时提出进一步的问题。