Javascript 无法读取属性';今天';未定义的

Javascript 无法读取属性';今天';未定义的,javascript,Javascript,我得到Uncaught(in promise)TypeError:无法读取此代码段中未定义的的属性“toDate”。触发这个错误的可能是什么?代码运行并在屏幕上显示我想要的内容,但我仍然得到这个错误 const pr1200 = document.querySelector('#comentarios-pb-preco'); function renderPreco1200(doc){ let li = document.createElement('li'); let name

我得到
Uncaught(in promise)TypeError:无法读取此代码段中未定义的
的属性“toDate”。触发这个错误的可能是什么?代码运行并在屏幕上显示我想要的内容,但我仍然得到这个错误

const pr1200 = document.querySelector('#comentarios-pb-preco');

function renderPreco1200(doc){
   let li = document.createElement('li');
   let name = document.createElement('span');
   let dat = document.createElement('span');

  if (doc.data() == "X"){
     console.log("Do not get this Value");
  }
  else {
     li.setAttribute('data-id', doc.id);
  name.textContent = doc.data().Obs;
  dateNow = doc.data().timestamp;
  dat.textContent = dateNow.toDate(); //This line

   li.appendChild(name);
   li.appendChild(dat);

   pr1200.appendChild(li);
  }
}

db.collection('Preco1200').get().then(snapshot => {
   snapshot.docs.forEach(doc => {
       console.log(doc.data())
        renderPreco1200(doc);
    });
});

当dateNow未定义时,您可以通过将
textContent
设置为空字符串来解释
未定义的时间戳

dat.textContent = dateNow === undefined ? "" : dateNow.toDate();

这意味着在您的代码中,
doc.data().timestamp
正在返回
未定义的
。在该行上放置一个断点,或者使用
console.log()
打印
doc.data()
的值,并查看
timestamp
属性是什么。它说的检查中有一行是疯狂的{秒:1583170475,纳秒:839000000}秒数:1583170475纳秒:839000000从日志中,在另一行上,引用同一行,它表示未定义。正如我所说,它获取我想要的内容并显示在屏幕上,但也给出了该错误。@Phacer-您正在为每个快照运行
renderPreco1200
。其中一个快照没有时间戳,其他快照有。您可以当
dateNow
undefined
时,通过将
textContent
设置为空字符串来计算
undefined
时间戳:dateNow.toDate();