我需要更新文档(如果存在),否则创建 [英] I need to update document if exists otherwise create

查看:51
本文介绍了我需要更新文档(如果存在),否则创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在对文档进行任何更新的方法(如果存在的话)来创建文档.现在,我正在使用update方法,但是如果该文档不存在,它将抛出一条错误消息.

Is there any way to do an update to a document if exists else create a document. Now I am using the update method but if the document does not exist then it will throw an error message.

Firestore.instance.collection('partnerRequests').document(user.uid).updateData( {
                  DateTime.now().millisecondsSinceEpoch.toString():partnerRegistrationFormData})

未处理的异常:类型'PlatformException'不是类型的子类型 '字符串'

Unhandled Exception: type 'PlatformException' is not a subtype of type 'String'

推荐答案

如果您正在调用updateData()函数,则仅当partnerRequests集合中已经存在 的文档时,才更新文档. .如果该特定文档存在,则updateData()调用将失败,除非您向我们展示了.

If you are calling updateData() function, you'll only update the document if it already exists in your partnerRequests collection. If that particular document does not exist, the updateData() call will fail, with the exception you showed us.

另一方面,如果您要调用setData()函数:

On the other hand, in case you are calling setData() function:

docRef.setData(data, merge: true)

如果文档不存在,则将其创建;如果文档已存在,则将对其进行更新.这基本上就是这两个功能之间的区别.

You'll create the document if it doesn't exist, or update it if it's already there. That's basically the difference between these two functions.

并回答您的问题:

如果存在,我需要更新文档,否则创建

I need to update the document if exists otherwise create

您绝对应该使用setData().

这篇关于我需要更新文档(如果存在),否则创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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