javascript - 为什么 snap.data().id 没有定义?
问题描述
我似乎无法获得要检索的文档的 ID。
我在网上看了很多例子,他们似乎都在做我正在做的事情。
exports.moveToProfile = functions.firestore
.document("tempProfiles/{id}")
.onCreate(async (snap, context) => {
const id = snap.data().id;
const displayName = snap.data().displayName;
const profile = await db
.collection("profiles")
.doc(id)
.set({
displayName: displayName,
points: 0
});
return profile;
});
解决方案
在您的代码中,data
是DocumentSnapshot类型的对象。从链接的 API 文档中可以看到,该对象表示的文档的 ID 是它的id 属性。data() 为您提供其所有字段(正式文档 ID 不是其中之一,除非您将其写为字段。)因此,您可以使用data.id
.
推荐阅读
- node.js - 尝试创建新的角度项目时出错
- regex - pandoc生成的markdown文件中的非贪婪正则表达式匹配太多
- google-chrome - 如何使用巧克力在自定义位置安装 chrome
- reactjs - 当我在 componentDidMount 中使用异步时,组件会一次又一次地挂载和卸载。为什么?
- recursion - 多个可滚动小部件的滚动同步
- python - 如何将字典值设置为像实例属性一样?
- docker - 需要回答dockerfile中的安装问题
- asp.net-core - 使用 fullcalendar 从 razor post 获取 json 的错误请求
- excel - Excel 公式 IF(AND?
- node.js - 尝试连接到服务器时,我收到断言 `args[1]->IsString()' failed 错误