简体   繁体   English

写入 Firebase Cloud Firestore

[英]Writing to Firebase Cloud Firestore

Is writing to firebase cloud firestore asynchronous in javascript?是否在 javascript 中异步写入 firebase Cloud Firestore? And if it is, what is a way to deal with it?如果是,有什么方法可以处理它?

My program is supposed to update a value into the database, and then pull that value out soon after, to do something with it.我的程序应该将一个值更新到数据库中,然后在不久之后将该值拉出,以对其进行处理。

Most of the times, the correct value comes out, however, sometimes the previous value is outputted, but when the page is refreshed and the same value is read from the database again, the correct value gets outputted.大多数时候会输出正确的值,但有时会输出之前的值,但是当刷新页面并再次从数据库中读取相同的值时,会输出正确的值。

So, I am assuming the issue must be that writing to the database is asynchronous, just like reading a database is.所以,我假设问题一定是写入数据库是异步的,就像读取数据库一样。

".then" didn't seem to work. “.then”似乎不起作用。

Here is a simplified version of my code:这是我的代码的简化版本:

function updateDB{
 db.collection("rooms").doc("roomsDoc").update({
   roomTime: timeInput  //timeInput is a variable defined (not shown in code here)              
  }).then
  {
   readDB();
  }
}

function readDB(){
   db.collection("rooms").doc("roomsDoc").get().then(function(doc) {
     console.log(doc.data().roomTime);
   });
}

The console.log is what outputs the wrong value sometimes. console.log 有时会输出错误的值。

yes it is asynchronous.是的,它是异步的。

a good way to handle the problem you are having is to async/await .处理您遇到的问题的一个好方法是async/await

first, you need to make the function you are doing this in an async function首先,您需要在async function 中制作 function

then do something like this然后做这样的事情

async function FunctionName(){
    // do some initial stuff
    await write to the database
    // do some intermediate stuff
    await read from the database
}

All JavaScript client/browser APIs that work with files and networks are asynchronous - that is the nature of JavaScript.所有与文件和网络一起工作的 JavaScript 客户端/浏览器 API 都是异步的 - 这是 JavaScript 的本质。

then() does, in fact, work on the promises returned by Firestore's APIs (and all promises, for that matter).实际上, then()确实对 Firestore 的 API 返回的承诺(以及所有承诺,就此而言)起作用。 There are lots of examples of this in thedocumentation . 文档中有很多这样的例子。

Without seeing your code, we don't know what you might be doing wrong.如果没有看到您的代码,我们不知道您可能做错了什么。

Yes, it is asynchronous.是的,它是异步的。

You should check the official documentation regarding this topic, it was very useful to me when trying to fetch data from the database/API:您应该查看有关此主题的官方文档,它在尝试从数据库/API 获取数据时对我非常有用:

Firebase Cloud Firestore Firebase 云防火墙

First you have to initialize your document ref and fetch data with get() async function and wait for the response using either then() or async/await method.首先,您必须使用get() async function 初始化文档 ref 并获取数据,然后使用then()async/await方法等待响应。

Hope this helps!希望这可以帮助!

I think you made a typo here:我想你在这里打错了:

.then
  {
   readDB();
  }

in this way, you are not really waiting for the completion of the first operation.这样,您就不会真正等待第一个操作的完成。 To get what you want you need to write:要得到你想要的,你需要写:

.then(() => readDB())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM