Node.js Firestore查询有时不支持';不包含更新的数据

Node.js Firestore查询有时不支持';不包含更新的数据,node.js,firebase,google-cloud-firestore,firebase-admin,Node.js,Firebase,Google Cloud Firestore,Firebase Admin,我正在使用firestore的节点sdk。在我的代码中,我调用function1,它更新firestore中的一个表。当该函数结束时,我调用function2,它运行一个查询以获取表的引用。大约80%的时间它都能工作,但有时我需要的数据(在function1中添加到文档中)不会在快照中返回,因此会抛出错误 我在更新之前添加了一个wait关键字,但这似乎并没有使代码等待firestore更新完成 我想我也可以返回我要在function1中更新的数据,并将其传递到function2,但这感觉有点像黑

我正在使用firestore的节点sdk。在我的代码中,我调用function1,它更新firestore中的一个表。当该函数结束时,我调用function2,它运行一个查询以获取表的引用。大约80%的时间它都能工作,但有时我需要的数据(在function1中添加到文档中)不会在快照中返回,因此会抛出错误

我在更新之前添加了一个wait关键字,但这似乎并没有使代码等待firestore更新完成

我想我也可以返回我要在function1中更新的数据,并将其传递到function2,但这感觉有点像黑客,尽管我想我会节省一点钱,因为我不再需要获取1个文档。我也可以把它做成一个大函数,但那会使它变成一个100行函数

以下是我的代码的缩写版本:

const function1 = async (tableId) => {
      const firestore = admin.firestore();
      const tableSnapshot = await firestore.collection('tables').doc(tableId).get();
      await tableSnapshot.ref.update({ smallBlind: {seat: 1, amount: 5000} }) // the seat number and amount number wont always be 1 and 5000. Otherwise I wouldn't need to look it up in function2
}

const function2 = async (tableId) => {
      const firestore = admin.firestore();
      const tableSnapshot = await firestore.collection('tables').doc(tableId).get();
      const tableData = tableSnapshot.data();
      const smallBlind = tableSnapshot.data().smallBlind; // the smallBlind data is not there. so smallBlind is undefined
}

const startStuff = async () => {
   await function1(42); // example tableId is passed in
   await function2(42);
}
startStuff()

上面的代码没有异步问题。我在代码的另一部分遇到了一个不同的异步问题,这导致了我的问题。

如果你在代码中采用
承诺,那么()方法也会发生这种情况吗?@ralemos我没有尝试过,但由于异步/等待是承诺的语法糖衣,我不明白为什么这会有关系。你是对的,我建议这样做,因为这似乎是一个同步性问题,使用这种方法进行故障排除会更容易。