【问题标题】:How acess the data inside a document triggered in cloud firestore? / Does this read charge?如何访问在 Cloud Firestore 中触发的文档中的数据? / 这个阅读收费吗?
【发布时间】:2018-08-28 14:59:10
【问题描述】:

当用户在帖子中遇到时,我想激活云功能。我想通过云功能访问此评论。

示例:帖子 X 中的用户评论: '我想吃鱼'。

我想通过云功能访问->帖子ID,评论ID和评论中的数据('我想吃鱼')

我有以下云功能:

exports.onCommentarie = functions.firestore .document('posts/{postId}/{commentaries}/{commentarieId}') .onCreate((context) => {
let postId = context.params.postId
let commentarieId = context.params.commentarieId
let data = context
console.log('O comentário do Id foi:', data)
console.log('o Id do post foi:', postId)
console.log('o Id do comentário foi:', commentarieId) })

如果我使用.onCreate((change, context) => 控制台返回错误。

我可以通过上下文访问数据吗?我的上下文正在返回:

Ocommentário do Id foi: { data: QueryDocumentSnapshot { _ref: DocumentReference { _firestore: [对象], _validator: [对象], _referencePath: [Object] }, _fieldsProto: { comment: [Object], creatorId: [Object], creatorName: [Object], creatorPhoto: [Object], 日期:[对象]},_serializer:序列化器{ timestampsInSnapshotsEnabled: false, createReference: [Function] }, _validator: 验证器 { isFunction: [Function], isOptionalFunction: [Function], isInteger: [Function], isOptionalInteger: [Function], isNumber: [函数], isOptionalNumber: [函数], isObject: [Function], isOptionalObject: [Function], isString: [Function], isOptionalString: [Function], isBoolean: [Function], isOptionalBoolean: [函数], isArrayElement: [函数], isOptionalArrayElement: [Function], isDeletePrecondition: [Function], isOptionalDeletePrecondition: [Function], isDocument: [Function], isOptionalDocument: [Function], isDocumentReference: [Function], isOptionalDocumentReference: [Function], isFieldPath: [Function], isOptionalFieldPath: [Function], isFieldValue: [Function], isOptionalFieldValue: [Function], isFieldOrder: [Function], isOptionalFieldOrder: [Function], isQueryComparison: [Function], isOptionalQueryComparison: [Function], isQueryValue: [Function], isOptionalQueryValue: [Function], isResourcePath: [Function], isOptionalResourcePath: [Function], isSetOptions: [Function], isOptionalSetOptions: [功能], isUpdateMap: [功能], isOptionalUpdateMap: [函数], isUpdatePrecondition: [函数], isOptionalUpdatePrecondition:[功能] },_readTime:未定义, _createTime:时间戳{ _seconds:1535341163,_nanoseconds:46815000},_updateTime:时间戳{_seconds:1535341163,_nanoseconds: 46815000 } }, eventId: '168433e5-0a62-42a5-a7b4-dbd58f629661-0', eventType: 'providers/cloud.firestore/eventTypes/document.create', 不支持:{},参数:{ postId:'U4lpN89JeM8cSVl2sv95', 评论:“评​​论”,commentarieId:“F85CRjInT1HPltEbbKJS”}, 资源: 'projects/herot-eyes/databases/(default)/documents/posts/U4lpN89JeM8cSVl2sv95/commentaries/F85CRjInT1HPltEbbKJS', 时间戳:'2018-08-27T03:39:23.046815Z' }

if (change, context) 返回错误,如何访问其中的数据?!

此阅读算作账单中的阅读?!我得计算一下,因为这会增加很多服务器的成本。

【问题讨论】:

    标签: javascript firebase compiler-errors google-cloud-firestore google-cloud-functions


    【解决方案1】:

    Firestore 的 onCreate() 触发器的语法如下,文档中详细说明了 here

    exports.createUser = functions.firestore
        .document('users/{userId}')
        .onCreate((snap, context) => {
          // Get an object representing the document
          // e.g. {'name': 'Marie', 'age': 66}
          const newValue = snap.data();
    
          // access a particular field as you would any JS property
          const name = newValue.name;
    
          // perform desired operations ...
        });
    

    您在代码中只传递了一个参数 (.onCreate((context) => {})),因此您必须

    1. 修改它以传递两个参数
    2. let data = context 修改为const data = snap.data()(或const data = change.data(),如果您使用.onCreate((change, context))。

    无法通过context 获取数据。

    上下文参数提供有关函数的信息 执行。跨异步函数类型、上下文相同 包含字段 eventId、timestamp、eventType、resource 和 参数。

    【讨论】:

    • 你的答案似乎是对的,但是......这就是我所尝试的,正是这样。而且,正如我在问题中所说,它没有用。出于某种原因,放置 (snap, context) 会出错。我只使用上下文来解决。使用:context.data.data()
    • 您确定将let data = context 修改为const data = snap.data()
    【解决方案2】:

    @RenaudTarnec 的答案似乎是一个很好的答案,也是解决此类问题的方法。尽管如此,@eeerrrttt 通过使用context.data.data() 而不是context 来解决data 变量的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-26
      • 2019-08-13
      • 2023-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      相关资源
      最近更新 更多