angular - 获取 doc.id 作为响应以及集合中的数据
问题描述
从集合中获取数据leads
并显示 Html 表上的所有文档。单击时,我需要获取单击项目的 doc.id,因为它是唯一的主键。
PS我正在获取数组中所有文档的记录。我也需要在同一个数组中的文档 ID。
下面是我用来获取数据的代码。我正在推动tableData
阵列。我想要这个数组中的 docid。
this.firestore.collection('leads' , ref => ref
.limit(5)
).snapshotChanges()
.subscribe(response => {
if(!response.length){
console.log("no data available");
return false;
}
this.firstInResponse = response[0].payload.doc;
this.lastInResponse = response[response.length - 1].payload.doc;
this.tableData = [];
for(let item of response){
this.tableData.push(item.payload.doc.data());
console.log(this.tableData);
}
...
为了清楚起见,附上 console.log 和 Firestore 的屏幕截图。
编辑 1
在尝试这个之后
for(let item of response){
var data = item;
Object.assign(data, {id : item.payload.doc.id})
this.tableData.push(data);
console.log(this.tableData);
我的实际数组坏了,下面是截图
解决方案
尝试这个,
for(let item of response){
var data = Object.assign(item.payload.doc.data(), {id : item.payload.doc.id})
this.tableData.push(data);
console.log(this.tableData);
}
我从中获取了 id item.payload.doc.id
,然后将 id 属性添加到数据对象,然后将修改后的对象推送到 tableData
希望这可以帮助!!
推荐阅读
- github - Embed readme file snippet into another one
- git - Does the repo I git clone automatically push using those credentials if I have same SSH key on both accounts?
- azure-devops - Azure DevOps YAML CI 和 CD 管道
- numpy - 在 Numpy 中组合矩阵以保留外部维度
- php - 从 select 中随机选择选项并相应地显示输出
- javascript - div 元素 onkeypress 无法正常工作
- java - 从java中的s3读取文件parquet文件
- powershell - 导入用户和管理员的 csv 文件的最佳方式,然后通过电子邮件向管理员发送用户列表
- r - tidyverse:将 data.frame 的每一行与另一个 data.frame 的单行进行比较
- python - 不同类下同名,获取URL,BeautifulSoup Python