Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.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 为什么Firestore在云函数内部查询时返回不一致的结果?_Node.js_Firebase_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

Node.js 为什么Firestore在云函数内部查询时返回不一致的结果?

Node.js 为什么Firestore在云函数内部查询时返回不一致的结果?,node.js,firebase,google-cloud-firestore,google-cloud-functions,Node.js,Firebase,Google Cloud Firestore,Google Cloud Functions,我的目标是创建一个幂等云函数来更新某些文档的计数器 我不是增加或减少计数器,而是查询文档并用结果总数更新计数器 const db = admin.firestore(); export const unprocessedReceiptsCount = functions.firestore.document('Purchases/{purchaseId}') .onWrite(async (snapshot, context) => { const unprocessedRe

我的目标是创建一个幂等云函数来更新某些文档的计数器

我不是增加或减少计数器,而是查询文档并用结果总数更新计数器

const db = admin.firestore();

export const unprocessedReceiptsCount = 
functions.firestore.document('Purchases/{purchaseId}')
.onWrite(async (snapshot, context) => {

    const unprocessedRef = db.collection('Purchases').where('hasBeenProcessed', '==', false);
    const unprocessedSnap = await unprocessedRef.get();
    const count = unprocessedSnap.docs.length;
    console.log(count);
    return count;

});
当我在任何文档中更新hasBeenProcessed字段时,它应该记录一个一致的编号。然而,我得到的结果遍布董事会:2,63,8,44。每次函数运行时,它都会显示一个看似随机的数字


我做错了什么?

最后,这是一个答案:

谷歌的人说:

此错误是由此问题引起的。由于grpc js 0.4.1目前尚未发布,我们的工程师建议删除节点_模块并再次尝试安装

他们的解决方案对我不起作用,但这确实起了作用:

npm uninstall @grpc/grpc-js
npm install @grpc/grpc-js@0.4.0
这也解决了云函数中的问题

----原始评论---

我也有同样的问题。这在上周运行得很好,现在即使是没有限制的简单get()也会返回随机数目的结果

const db = admin.firestore();

export const unprocessedReceiptsCount = 
functions.firestore.document('Purchases/{purchaseId}')
.onWrite(async (snapshot, context) => {

    const unprocessedRef = db.collection('Purchases').where('hasBeenProcessed', '==', false);
    const unprocessedSnap = await unprocessedRef.get();
    const count = unprocessedSnap.docs.length;
    console.log(count);
    return count;

});
下面的代码演示了AdminSDK的问题。我已经向谷歌提交了一份bug报告

var count = 10
getAgain()
function getAgain() {
  admin.firestore().collection('activity').get().then((qs) => {
    console.log("activities: got " + qs.docs.length)
    count--
    if (count) getAgain()
  })
}

CONSOLE LOG:
activities: got 24
activities: got 6
activities: got 6
activities: got 2
activities: got 11
activities: got 6
activities: got 2
activities: got 12
activities: got 13
activities: got 14


嗨,托马斯,欢迎来到堆栈溢出。答案不是评论,你可能希望删除它,因为它将很快被否决。谢谢你的评论,托马斯。正如Tim所建议的,请删除此答案,并将其作为对主要问题的评论添加到更合适的地方。此外,+1是主要问题,因此我们可以获得可见性,并可能找到解决方案。谢谢我没有足够的声誉来评论最初的问题。