javascript - 在 Firestore 中设置/添加操作后返回文档数据
问题描述
我正在尝试取回有关我刚刚创建/更新的文档的一些信息,而无需再次请求。
我知道我得到了带有文档 ID 的文档引用,但我需要其他信息,例如 createTime 和 updateTime。
目前,这是我试图实现这一目标的方式,但我想放弃那个额外的请求,因为它不是很有效。
const docRef = await database.collection('tagsCollection').add({
...input
createTime: firestore.FieldValue.serverTimestamp()
});
const doc = await docRef.get();
return {
...doc.data(),
id: doc.id,
createTime: doc.data().createTime.toMillis().toString()
};
解决方案
由于这些字段来自服务器端的 Firestore,因此可以安全地假设客户端在查询 DocumentSnapshot 之前不知道它们是什么。您也许可以自己猜测,但实际上您是在尝试猜测调用add()
.
推荐阅读
- java - 如果语句没有被执行,即使它是真的
- python - 四方代码错误:TypeError:只能将 str(不是“元组”)连接到 str
- c# - 如何从另一个类中的索引器访问 Main() 方法中的元素?
- php - 如何在 php 中的 post 中找到 hashtag 和 tag 和 url?
- react-native - AutoHeightWebView 的 React-native Display 导致 Android 10 和 Android 11 崩溃
- javascript - JavaScript - 我想显示一个指示用户等待进程完成的模式
- css - 是否有任何库支持放大/缩小角度 10+ 的角度 div 或文本或图像
- c - 我的代码有什么问题我无法在 vscode 中使用 scanf
- r - 为什么这个产品不产生 pi 的值?
- facebook-graph-api - Facebook Graph API - ID 匹配 API 是否适用于合作伙伴?