【发布时间】:2023-03-31 04:36:01
【问题描述】:
我最近将我的谷歌云功能升级到 Node.js 10。我在触发云功能的新集合中遇到了奇怪的隐藏文档。这些是文件:
_createTime
_fieldsProto
_readTime
_ref
_序列化器
_updateTime
我的代码中不存在这些文档字符串名称。
但它们确实存在于 DocumentSnapshot 的构造函数中:googleapis.dev/nodejs/firestore/latest/document.js.html
我有一个云函数,它通过示例路径 ('followers/{userId}/userFollowers/{followerId}') 触发文档 onCreate。现在有 3 次新用户和第一次创建此子集合。
作为临时解决方法,我告诉函数忽略与这些隐藏文档名称匹配的 [followerId]。但是,我有 30 多个在创建文档时触发的函数,我不希望在每个函数的顶部都永久使用这种 hack 解决方法。
有其他人经历过吗?有什么想法吗?
这是我跟随用户的 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'
【问题讨论】:
-
1) 节点 10 的工作方式与节点 8 根本没有任何不同。2) Firestore 中没有“隐藏文档”之类的东西。所有文档都同样可见,无论其 ID 是什么。在没有看到您的代码和源数据的情况下,我们无法诊断这里发生的任何事情。请编辑问题以更清楚地说明您如何能够重现该行为,以及您所期望的。
-
添加了 Dart 中的 follow 函数和云函数 sn-p 的代码。我看不到跟随函数上方的问题,因为'getUserRef(fid:“_serializer”,uid:currentUser.id)'文档不存在并且会退出该函数。这个问题也只发生在第一次创建这个子集合时。
-
“做事”需要显示。请提供足够的信息,以便任何人都可以重现该问题并观察您正在观察的内容。
-
我建议尝试仅使用记录文档 ID 的最小云函数来重现问题。如果您不能像那样重现它,请添加一些可能导致问题的代码。继续执行此操作,直到您重现问题,并仅使用 that 代码更新查询。另见how to create a minimal, complete, verifiable example。
-
googleapis.dev/nodejs/firestore/latest/document.js.html 在这里您可以看到firestore上文档的构造函数中存在的每个字段
标签: firebase flutter dart google-cloud-firestore google-cloud-functions