javascript - 如何添加文档并将其 ID 立即存储在 Firebase 的其他地方?使用本机反应
问题描述
我正在尝试在我的相册集合中创建一个文档,然后获取创建的新文档的 ID 并将其存储在我的 Firestore 的其他位置。这是我的代码和错误消息。
const createAlbum = () => {
firebase
.firestore()
.collection("albums")
.add(albumName)
.then((snapshot) => {
let newAlbum = snapshot.docs.map((doc) => {
const id = doc.id;
return id;
});
for (let i = 0; i < usersSelected.length; i++) {
firebase
.firestore()
.collection("following")
.doc(usersSelected[i].id)
.collection("userFollowing")
.doc(newAlbum.id)
.set({});
}
});
};
[未处理的承诺拒绝:TypeError:未定义不是对象(评估'snapshot.docs.map')]在[本机代码]:在[本机代码]的flushedQueue中为null:在callFunctionReturnFlushedQueue中为null
解决方案
尝试这样的事情:
const createAlbum = () => {
firebase
.firestore()
.collection("albums")
.add(albumName)
.then((doc) => {
const id = doc.id;
for (let i = 0; i < usersSelected.length; i++) {
firebase
.firestore()
.collection("following")
.doc(usersSelected[i].id)
.collection("userFollowing")
.doc(id)
.set({ });
}
});
}
推荐阅读
- javascript - 当我们通过单击事件切换另一个类时如何删除先前切换的类
- python - ansible --version: AttributeError: 'module' 对象没有属性 'DEFAULT_LOCAL_TMP'
- c# - 从 ORACLE 数据库中选择数据时遇到 ORA-01830 错误
- python - 将复合值转换为 pandas 数据框中的列
- go - 如何在 *.proto 文件中定义一个空接口?
- heroku - 无法在 BigRock 中添加 Heroku DNS 目标
- php - 通过链接与 api 集成的商店(API Xtream 代码)
- java - 如何解决错误“不正确的整数值:第 1 行的列 'type_id' 的 'null'”?
- timer - 为什么命令在 SetTimer 之后不运行?
- ruby-on-rails - 我应该如何将哈希定义为枚举?