Firebase:云功能触发器:新集合中的六个隐藏的“ _xxxxx”文档触发了云功能

时间:2020-07-02 18:21:57

标签: firebase flutter dart google-cloud-firestore google-cloud-functions

我最近将我的Google云功能升级到Node.js10。在新集合中,我会遇到奇怪的隐藏文档事件,这些事件触发了云功能。这些是文档:

_createTime
_fieldsProto
_readTime
_ref
_serializer
_updateTime

这些文档中的字符串名称在我的代码中不存在。
但它们确实存在于DocumentSnapshot的构造函数中:googleapis.dev/nodejs/firestore/latest/document.js.html

我有一个云函数,它会触发带有示例路径('followers / {userId} / userFollowers / {followerId}'的文档onCreate。现在,新用户已经出现了三遍,并且第一次创建了这个子集合。

作为临时的解决方法,我告诉函数忽略与这些隐藏文档名称匹配的[followerId]。但是,我有30多个在创建文档时触发的功能,并且我不想在每个功能的顶部永久使用此变通办法。

还有其他人经历过吗?知道发生了什么事吗?

这是我跟随用户的Flutter / Dart代码:

followUser(String followerId) async {
    await getUserRef(uid: followerId).get().then((doc) {
      if (!doc.exists) return;
      documentUpdate(
          docRef: getFollowersRef(fid: followerId, uid: currentUser.id),
          payload: {
             //PAYLOAD EXAMPLE:
            'notificationToken': currentUser.notificationToken, //String
            'username': currentUser.username, //String
            'ofUsername': doc.data['username'], //String
            'profileImgURL': currentUser.photoUrl, //String
            'timestamp': DateTime.now(),  //Timestamp
            'displayName': currentUser.displayName, //String
          });
      documentUpdate(
          docRef: getFollowingRef(uid: currentUser.id, fid: followerId),
          payload: {
            // follower data. Same as above but inverted for follower.
          });
    });
  }

documentUpdate({DocumentReference docRef, Map<String, dynamic> payload}) {
   return docRef.setData(payload, merge: true);
}

来自index.js云功能的摘录:

exports.onNewFollower = functions.firestore
    .document('/followers/{userId}/userFollowers/{followerId}')
    .onCreate(async (snapshot, context) => {
        const userId = context.params.userId;
        const followerId = context.params.followerId;
        console.log(`uid: [${userId}], fid: [${followerId}]`);
        if (followerId == 'index' ||
            userId == '_ref' ||      //ADDED THIS AFTER FOR PREVENTION
            userId == '_fieldsPronto' ||
            userId == '_createTime' ||
            userId == '_readTime' ||
            userId == '_updateTime' ||
            userId == '_serializer' ||
            followerId == '_ref' ||
            followerId == '_fieldsPronto' ||
            followerId == '_createTime' ||
            followerId == '_readTime' ||
            followerId == '_updateTime' ||
            followerId == '_serializer')
          return await Promise.resolve(true);

        var promises = [];
        promises.push(db.collection('followers').doc(userId).collection('userFollowers').doc('index').set(
                { 'uids': { [followerId]: true } },
                { merge: true },
            )
        );
        promises.push(
            db.collection('following').doc(followerId).collection('userFollowing').doc('index').set(
                { 'uids': { [userId]: true } },
                { merge: true },
            )
        );
        promises.push(db.collection('activityFeed').doc(userId).collection('feedItems').doc(followerId).set(
            {
                timestamp: admin.firestore.FieldValue.serverTimestamp(),
                type: 'newFollower',
                userId: followerId,
                userProfileImg: snapshot.data().profileImgURL,
                username: snapshot.data().username,
                displayName: snapshot.data().displayName,
            }
        ));
        return await Promise.all(promises);
});

这里是尝试这些奇数文档时云功能控制台输出的示例。理解起来很混乱,但是很明显,这些奇怪的文件是在原先的文件触发后触发的。云端功能会引发错误,因为这些奇怪的文档不包含该功能需要完成的数据。

7:16:25.473 PM
onNewFollower
Function execution started
7:16:26.535 PM
onNewFollower
uid: [Du1orkZrykWJ1BL0kKOuj4HO0ji2], fid: [LtiIcZ8rrphcEnyCWnieKvte6ln2]
7:16:27.442 PM
onNewFollower
Function execution took 1971 ms, finished with status: 'ok'
7:17:14.663 PM
onNewFollower
Function execution started 
7:17:14.677 PM
onNewFollower
uid: [_ref], fid: [LtiIcZ8rrphcEnyCWnieKvte6ln2]
7:17:14.684 PM
onNewFollower
Function execution took 21 ms, finished with status: 'error'
7:17:15.342 PM
onNewFollower
Function execution started
7:17:15.352 PM
onNewFollower
Function execution took 11 ms, finished with status: 'ok'
7:17:15.659 PM
onNewFollower
Function execution started
7:17:15.664 PM
onNewFollower
uid: [_createTime], fid: [LtiIcZ8rrphcEnyCWnieKvte6ln2]
7:17:15.666 PM
onNewFollower
Error: Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field "username"). at Object.validateUserInput (/workspace/node_modules/@google-cloud/firestore/build/src/serializer.js:251:15) at validateDocumentData (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:610:22) at WriteBatch.set (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:232:9) at DocumentReference.set (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:338:14) at exports.onNewFollower.functions.firestore.document.onCreate (/workspace/index.js:842:99) at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:131:23) at Promise.resolve.then (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:330:28) at process._tickCallback (internal/process/next_tick.js:68:7)
7:17:15.667 PM
onNewFollower
Function execution took 31 ms, finished with status: 'error'
7:17:15.790 PM
onNewFollower
Function execution started
7:17:15.797 PM
onNewFollower
Function execution took 7 ms, finished with status: 'ok'
7:17:16.682 PM
onNewFollower
Error: Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field "username"). at Object.validateUserInput (/workspace/node_modules/@google-cloud/firestore/build/src/serializer.js:251:15) at validateDocumentData (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:610:22) at WriteBatch.set (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:232:9) at DocumentReference.set (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:338:14) at exports.onNewFollower.functions.firestore.document.onCreate (/workspace/index.js:842:99) at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:131:23) at Promise.resolve.then (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:330:28) at process._tickCallback (internal/process/next_tick.js:68:7)
7:17:17.467 PM
onNewFollower
Function execution started
7:17:17.616 PM
onNewFollower
Function execution started
7:17:17.948 PM
onNewFollower
Function execution started
7:17:18.288 PM
onNewFollower
Function execution started
7:17:18.664 PM
onNewFollower
uid: [_readTime], fid: [LtiIcZ8rrphcEnyCWnieKvte6ln2]
7:17:19.069 PM
onNewFollower
Function execution took 1603 ms, finished with status: 'error'
7:17:19.363 PM
onNewFollower
uid: [_updateTime], fid: [LtiIcZ8rrphcEnyCWnieKvte6ln2]
7:17:19.430 PM
onNewFollower
uid: [_serializer], fid: [LtiIcZ8rrphcEnyCWnieKvte6ln2]
7:17:19.863 PM
onNewFollower
Function execution took 2247 ms, finished with status: 'error' 
7:17:19.878 PM
onNewFollower
Function execution took 1931 ms, finished with status: 'error'
7:17:19.960 PM
onNewFollower
uid: [_fieldsProto], fid: [LtiIcZ8rrphcEnyCWnieKvte6ln2]
7:17:20.089 PM
onNewFollower
Error: Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field "username"). at Object.validateUserInput (/workspace/node_modules/@google-cloud/firestore/build/src/serializer.js:251:15) at validateDocumentData (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:610:22) at WriteBatch.set (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:232:9) at DocumentReference.set (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:338:14) at exports.onNewFollower.functions.firestore.document.onCreate (/workspace/index.js:842:99) at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:131:23) at Promise.resolve.then (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:330:28) at process._tickCallback (internal/process/next_tick.js:68:7)
7:17:20.417 PM
onNewFollower
Function execution took 2129 ms, finished with status: 'error'
7:17:20.923 PM
onNewFollower
Error: Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field "username"). at Object.validateUserInput (/workspace/node_modules/@google-cloud/firestore/build/src/serializer.js:251:15) at validateDocumentData (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:610:22) at WriteBatch.set (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:232:9) at DocumentReference.set (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:338:14) at exports.onNewFollower.functions.firestore.document.onCreate (/workspace/index.js:842:99) at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:131:23) at Promise.resolve.then (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:330:28) at process._tickCallback (internal/process/next_tick.js:68:7)
7:17:20.928 PM
onNewFollower
Error: Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field "username"). at Object.validateUserInput (/workspace/node_modules/@google-cloud/firestore/build/src/serializer.js:251:15) at validateDocumentData (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:610:22) at WriteBatch.set (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:232:9) at DocumentReference.set (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:338:14) at exports.onNewFollower.functions.firestore.document.onCreate (/workspace/index.js:842:99) at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:131:23) at Promise.resolve.then (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:330:28) at process._tickCallback (internal/process/next_tick.js:68:7)
7:17:21.428 PM
onNewFollower
Error: Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field "username"). at Object.validateUserInput (/workspace/node_modules/@google-cloud/firestore/build/src/serializer.js:251:15) at validateDocumentData (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:610:22) at WriteBatch.set (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:232:9) at DocumentReference.set (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:338:14) at exports.onNewFollower.functions.firestore.document.onCreate (/workspace/index.js:842:99) at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:131:23) at Promise.resolve.then (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:330:28) at process._tickCallback (internal/process/next_tick.js:68:7)
7:17:36.561 PM
onNewFollower
Function execution started
7:17:36.572 PM
onNewFollower
Function execution took 11 ms, finished with status: 'ok'
7:17:37.567 PM
onNewFollower
Function execution started
7:17:37.581 PM
onNewFollower
Function execution took 15 ms, finished with status: 'ok'
7:17:41.263 PM
onNewFollower
Function execution started
7:17:41.370 PM
onNewFollower
Function execution took 108 ms, finished with status: 'ok'
7:17:42.070 PM
onNewFollower
Function execution started
7:17:42.853 PM
onNewFollower
Function execution took 785 ms, finished with status: 'ok'
7:24:25.667 PM
onNewFollower
Function execution started
7:24:26.775 PM
onNewFollower
uid: [ilxBwWHVDiVJ2iR4AsfIxrpIUgb2], fid: [LtiIcZ8rrphcEnyCWnieKvte6ln2]
7:24:27.567 PM
onNewFollower
Function execution took 1901 ms, finished with status: 'ok'

成功编写了文档,并且没有显示奇数文档的集合 The document successfully written and the collection with none of the odd documents showing

1 个答案:

答案 0 :(得分:0)

我将此问题发布到FlutterFire团队。他们告诉我,这实际上是插件的问题,并且该修复程序将在软件包的后续更新中推出...