Abh*_*oni 3 firebase google-cloud-firestore
我想按照下面提到的代码将一个对象插入到 Firestore 文档中。但它抛出了错误
.update({
"comments": firebase.fieldValue.arrayUnion({
userId,
comment: data.comment,
createdAt: firebase.fieldValue.serverTimestamp(),
})
Run Code Online (Sandbox Code Playgroud)
错误:
FirebaseError:使用无效数据调用函数 FieldValue.arrayUnion()。FieldValue.serverTimestamp() 只能与 update() 和 set() 一起使用(在文档中找到...)
如何向推送的数组对象添加时间戳,或者是否有其他方法可以实现类似的结果?我从上面的错误中得到什么是我不能使用里面的时间戳arrayUnion
您不能用作FieldValue.serverTimestamp()arrayUnion 的值,因为该函数不返回 a ,而是返回要在 a或timestamp中使用的哨兵。为此,您需要获取实际的类值。获取此值的方式与本地 SDK 和管理 SDK 不同,因此根据您使用的内容,您可以执行以下操作:set()update()timestamp
对于管理 SDK:
import admin = require('firebase-admin');
const created_at = admin.firestore.Timestamp.now()
Run Code Online (Sandbox Code Playgroud)
对于本地 SDK:
import { Timestamp } from '@google-cloud/firestore';
const created_at = Timestamp.now()
Run Code Online (Sandbox Code Playgroud)
然后您可以通过执行以下操作来更新您的文档:
.update({
"comments": firebase.fieldValue.arrayUnion({
userId,
comment: data.comment,
createdAt: created_at,
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
682 次 |
| 最近记录: |