gpt4 book ai didi

javascript - 如何在cloud firestore中触发访问文档内的数据?/此读取是否收费?

转载 作者:行者123 更新时间:2023-12-01 01:44:41 26 4
gpt4 key购买 nike

我想在用户评论帖子时激活云功能。我想通过云功能访问这篇评论。

示例:用户在帖子 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) =>控制台返回错误。

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

O comentário do Id foi: { data: QueryDocumentSnapshot { _ref: DocumentReference { _firestore: [Object], _validator: [Object], _referencePath: [Object] }, _fieldsProto: { comment: [Object], creatorId: [Object], creatorName: [Object], creatorPhoto: [Object], date: [Object] }, _serializer: Serializer { timestampsInSnapshotsEnabled: false, createReference: [Function] }, _validator: Validator { isFunction: [Function], isOptionalFunction: [Function], isInteger: [Function], isOptionalInteger: [Function], isNumber: [Function], isOptionalNumber: [Function], isObject: [Function], isOptionalObject: [Function], isString: [Function], isOptionalString: [Function], isBoolean: [Function], isOptionalBoolean: [Function], isArrayElement: [Function], 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: [Function], isUpdateMap: [Function], isOptionalUpdateMap: [Function], isUpdatePrecondition: [Function], isOptionalUpdatePrecondition: [Function] }, _readTime: undefined, _createTime: Timestamp { _seconds: 1535341163, _nanoseconds: 46815000 }, _updateTime: Timestamp { _seconds: 1535341163, _nanoseconds: 46815000 } }, eventId: '168433e5-0a62-42a5-a7b4-dbd58f629661-0', eventType: 'providers/cloud.firestore/eventTypes/document.create', notSupported: {}, params: { postId: 'U4lpN89JeM8cSVl2sv95', commentaries: 'commentaries', commentarieId: 'F85CRjInT1HPltEbbKJS' }, resource: 'projects/herot-eyes/databases/(default)/documents/posts/U4lpN89JeM8cSVl2sv95/commentaries/F85CRjInT1HPltEbbKJS', timestamp: '2018-08-27T03:39:23.046815Z' }

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

这次阅读算作账单阅读吗?!我必须计算这个,因为这会增加很多服务器的成本。

最佳答案

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获取数据。

The context parameter provides information about the function's execution. Identical across asynchronous functions types, context contains the fields eventId, timestamp, eventType, resource, and params.

关于javascript - 如何在cloud firestore中触发访问文档内的数据?/此读取是否收费?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52060924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com