首页 > 解决方案 > 相当于Firestore中的.push?

问题描述

我正在尝试将以前使用实时数据库的 firebase 实现转换为使用 firestore,因为我喜欢集合的想法和使用它的好处。

如何在以下等效于 Firestore 中实施?

firebase.database().ref('documentPath').push()

标签: javascriptfirebasegoogle-cloud-firestore

解决方案


使用该功能时,在 Cloud Firestore 中具有与在 Firebase 实时数据库中相同的行为push(),就是让 Cloud Firestore 为您自动生成一个 ID。你可以通过调用这样的add()函数来做到这一点:

var addYourDoc = db.collection('documentPath').add({
  property_key: 'property_value',
}).then(ref => {
  console.log('document ID: ', ref.id);
});

控制台中的输出将是实际生成的 id。


推荐阅读