Firebase Firestore:脱机添加数据后获取文档ID [英] Firebase Firestore: get document ID after adding data offline

查看:160
本文介绍了Firebase Firestore:脱机添加数据后获取文档ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样向Firestore添加数据:

I add data to Firestore like this:

db
    .collection('foo')
    .add({foo: 'bar'})
    .then(docRef => {
      console.log('Added Foo: ', docRef.id)
      // do some stuff here with the newly created foo and it's id.
    })
    .catch(console.error)

在创建文档之后,我想使用新文档或特别是它的ID.该文档以有效ID存储在本地数据库中.

After the document creation, I would like to work with the new doc or specially it's ID. The document is stored in the local database with a valid ID.

但是在创建文档后如何获取ID?在数据与服务器同步之前,承诺将无法解决.

But how do I get the ID after the document creation? The promise will not be resolved until the data has synced with the server.

推荐答案

您甚至可以在本地保存之前获取ID.您只是使用这种方式来写数据.

You can get Id even before you are saving locally. You just use this way to write data.

      // Add a new document with a generated id.
     var newCityRef = db.collection("cities").doc();
      var id = newCityRef.key;
      // later...
       newCityRef.set(data);

对于Web,默认情况下禁用离线持久性.要启用持久性,请调用enablePersistence方法

For the web, offline persistence is disabled by default. To enable persistence, call the enablePersistence method

firebase.firestore().enablePersistence()
  .then(function() {
      // Initialize Cloud Firestore through firebase
      var db = firebase.firestore();
  })
  .catch(function(err) {
      if (err.code == 'failed-precondition') {
          // Multiple tabs open, persistence can only be enabled
          // in one tab at a a time.
          // ...
      } else if (err.code == 'unimplemented') {
          // The current browser does not support all of the
          // features required to enable persistence
          // ...
      }

要检查您是从服务器还是从缓存中接收数据,请在快照事件中使用SnapshotMetadata上的fromCache属性.如果fromCache为true,则数据来自缓存,并且可能是陈旧的或不完整的.如果fromCache为false,则数据是完整的并且是服务器上的最新更新的最新信息.

To check whether you're receiving data from the server or the cache, use the fromCache property on the SnapshotMetadata in your snapshot event. IffromCache is true, the data came from the cache and might be stale or incomplete. If fromCache is false, the data is complete and current with the latest updates on the server.

默认情况下,仅更改SnapshotMetadata不会引发任何事件.如果您依赖fromCache值,则在附加侦听处理程序时指定includeMetadataChanges侦听选项.

By default, no event is raised if only the SnapshotMetadata changed. If you rely on the fromCache values, specify the includeMetadataChanges listen option when you attach your listen handler.

db.collection("cities").where("state", "==", "CA")
  .onSnapshot({ includeQueryMetadataChanges: true }, function(snapshot) {
      snapshot.docChanges.forEach(function(change) {
          if (change.type === "added") {
              console.log("New city: ", change.doc.data());
          }

          var source = snapshot.metadata.fromCache ? "local cache" : "server";
          console.log("Data came from " + source);
      });
  });

因此,如果您添加新数据并且启用了脱机功能,则数据将被添加到缓存中,并且可以被侦听器侦听.

So if you add new data and you have offline enabled your data will be added to cache and can be listened by the listeners.

});

这篇关于Firebase Firestore:脱机添加数据后获取文档ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆