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();