问题描述
我正在尝试将以前使用实时数据库的firebase实现转换为使用firestore,因为我喜欢收集的想法和使用它的好处。
I am trying to convert my firebase implementation which previously used realtime database to use firestore as I like the idea of collections and the perks of using it.
如何做我在下面实现了firestore等价物?
How do I implement below into firestore equivalent?
firebase.database().ref('documentPath').push()
推荐答案
在Cloud Firestore中拥有与Firebase Realtime中相同的行为使用 push()
函数时,数据库是让Cloud Firestore为您自动生成ID。您可以通过调用 add()
这样的函数来执行此操作:
To have the same bahaviour in Cloud Firestore as you have in Firebase Realtime database when using the push()
function, is to let Cloud Firestore auto-generate an ID for you. You can do this by calling add()
function like this:
var addYourDoc = db.collection('documentPath').add({
property_key: 'property_value',
}).then(ref => {
console.log('document ID: ', ref.id);
});
控制台中的输出将是实际生成的ID。
The output in the console will be the actual generated id.
这篇关于在Firestore中相当于.push?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!