首页 > 解决方案 > 如果 Firestore 中的数字不是字符串,则无法打印字段值

问题描述

我需要将其打印在列表中:

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:“日期/错误修复”

这就是我想在列表上打印的内容:

在此处输入图像描述

标签: javascripthtmlfirebasegoogle-cloud-firestore

解决方案


其他答案是正确的,但我还想补充一点,您的项目中可能存在一些转换问题,如果是这种情况,您必须将数字转换为字符串,一个简单的解决方法是

name.textContent = "" + updatesArgument.data()['count'];

或者

name.textContent = "" + updatesArgument.data()[1];

推荐阅读