我正在做一些测试以进入 Firebase。但现在当我尝试访问我的时间戳对象时,它返回未定义。
这是我尝试注销其日期的数组项(配方)之一的示例,从控制台复制:
{author: "xan", created at: n, title: "awesome test"}
author: "xan"
created at: n
nanoseconds: 0
seconds: 1563660000
__proto__:
isEqual: ƒ (t)
toDate: ƒ ()
toMillis: ƒ ()
toString: ƒ ()
_compareTo: ƒ (t)
constructor: ƒ n(t,e)
__proto__: Object
这是我的代码:
const list = document.querySelector('ul');
const addRecipe = (recipe) => {
let time = recipe.created_at.toDate();
console.log(recipe.created_at); // returns undefined ??
let html = `
<li>
<div>${recipe.title}</div>
<div>${time}</div>
</li>
`;
list.innerHTML += html;
}
db.collection('recipes').get().then(snapshot => {
snapshot.docs.forEach(doc => {
addRecipe(doc.data());
});
}).catch(err => {
console.log(err);
});
为什么recipe.created_at
返回未定义?
最佳答案
看起来您的数据库字段被称为“created at”,单词之间有一个空格,但您希望它是“created_at”,单词之间有一个下划线。确保读取文档的代码也与写入文档的代码相匹配。
关于javascript - 为什么 Firebase 时间戳对象返回未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57140790/