你能将时间戳转换为FieldValue吗?

huangapple go评论62阅读模式
英文:

Can you convert a Timestamp to a FieldValue?

问题

在我的应用中,用户将会上传一个文档到他们的子集合下的数据库,并附带一个时间戳到这个文档上。在我的安全规则中,我已经编写了代码,以便时间戳始终等于FieldValue.serverTimestamp()。这会将一个FieldValue保存到数据库中。

问题是,每当我从Firebase获取文档快照并访问字段时,它会返回一个时间戳而不是一个FieldValue。有没有办法将时间戳转换为FieldValue,或反之亦然?

英文:

In my app, user will upload a document to the database under their subcollection, with a Timestamp attached to the document. In my Security Rules, I have it programmed so that the Timestamp will always be equal to FieldValue.serverTimestamp(). This saves a FieldValue to the database.

The problem is, whenever I get the documentSnapshot back from Firebase and access the field, it comes back as a Timestamp and not a FieldValue. Is there a way I can convert a Timestamp to a FieldValue, or vice-versa?

答案1

得分: 0

不,你确实需要那个时间戳而不是一个FieldValue。 FieldValue 令牌仅用于编写需要在Firestore后端而不是在客户端上计算其实际值的字段值。 它们只是令牌,不是实际值。 当你获取文档并读取值时,它将具有实际的时间戳值(或最终值是什么),这是你需要使用的内容。

英文:

No, you really do want that Timestamp instead of a FieldValue. FieldValue tokens are only used for writing field values that need to have their actual values computed on the Firestore backend instead of on the client. They are just tokens, not actual values. When you fetch the document and read the value back out, it will have the actual Timestamp value (or whatever the final value is), which is what you need to work with.

huangapple
  • 本文由 发表于 2020年8月10日 07:18:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63332260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定