【问题标题】:Cant Print the field values if its a number not string in Firestore如果 Firestore 中的数字不是字符串,则无法打印字段值
【发布时间】:2021-09-12 00:46:01
【问题描述】:
我需要把它打印在一个列表上:
li.setAttribute('data-id', updatesArgument.id);//'id' here unique id
Example 1
name.textContent = updatesArgument.data().count;// Works if String
Example 2
let i=1;
name.textContent = updatesArgument.data().i;//Does not work if Number
li.appendChild(name);
infoUpdates.appendChild(li);
图片将向您展示
1:“日期/错误修复”
这就是我想在列表上打印的内容:
【问题讨论】:
标签:
javascript
html
firebase
google-cloud-firestore
【解决方案1】:
其他答案都是正确的,但我还想补充一点,您的项目中可能存在一些转换问题,如果是这种情况,您必须将数字转换为字符串,一个简单的解决方法是
name.textContent = "" + updatesArgument.data()['count'];
或
name.textContent = "" + updatesArgument.data()[1];
【解决方案2】:
我认为您没有为 firebase 维护正确的数据建模。无论如何,问题是,当您将numbers 用作object property 时,您应该像访问数组索引一样访问它。
试试
let i=1;
name.textContent = updatesArgument.data()[i];
虚拟示例
const obj = {
1: "Hello",
2: "Bro",
count: 8
};
console.log(obj[1]);
console.log(obj[2]);
console.log(obj["count"]);
console.log(obj.count);