Firebase Firestore在事务内添加新文档-transaction.add不是函数 [英] firebase firestore addding new document inside a transaction - transaction.add is not a function

查看:107
本文介绍了Firebase Firestore在事务内添加新文档-transaction.add不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为可以做类似的事情:

I was assuming that it was possible to do something like:

transaction.add(collectionRef,{
  uid: userId,
  name: name,
  fsTimestamp: firebase.firestore.Timestamp.now(),
});

但显然不是:

transaction.add不是函数

transaction.add is not a function

上述消息显示在chrome控制台内部.

The above message is displayed inside the chrome console.

我看到我们可以使用事务的 set 方法来以事务方式添加新文档.请参阅: https://firebase.google.com/docs/firestore/manage-data /transactions

I see that we can use the set method of the transaction to add a new document transactionally. see: https://firebase.google.com/docs/firestore/manage-data/transactions

问题是,如果我使用 set 而不是add(始终不支持),则文档ID应该由我手动创建,firestore不会创建. 请参阅: https://firebase.google.com/docs/firestore/manage -data/add-data

The thing is if I use set instead of add(which is not supported anyways), the id of the document should be created by me manually, firestore won't create it. see: https://firebase.google.com/docs/firestore/manage-data/add-data

您是否看到没有可自动为您生成ID的add方法的缺点?

Do you see any downside of this not having an add method that generates the id for you automatically?

例如,考虑到包括性能在内的各种问题,firestore本身生成的id是否有可能被优化了?

For example, is it possible that the id generated by the firestore itself is somehow optimized considering various concerns including performance?

在使用transaction.set时,您使用哪种库/方法在react-native中创建文档ID?

Which library/method do you use to create your document IDs in react-native while using transaction.set?

谢谢

推荐答案

如果要生成唯一的ID供以后在事务中创建文档时使用,则只需使用

If you want to generate a unique ID for later use in creating a document in a transaction, all you have to do is use CollectionReference.doc() with no parameters to generate a DocumentReference which you can set() later in a transaction.

(您在答案中提出的建议是,为达到相同的效果需要做更多的工作.)

(What you're proposing in your answer is way more work for the same effect.)

// Create a reference to a document that doesn't exist yet, it has a random id
const newDocRef = db.collection('coll').doc();

// Then, later in a transaction:
transaction.set(newDocRef, { ... });

这篇关于Firebase Firestore在事务内添加新文档-transaction.add不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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