Javascript 为什么Firebase时间戳对象返回未定义?
我正在做一个小测试来进入Firebase。但现在,当我试图访问timestamp对象时,它返回未定义 这是我试图注销日期的一个数组项(配方)的示例,从控制台复制:Javascript 为什么Firebase时间戳对象返回未定义?,javascript,firebase,google-cloud-firestore,nosql,timestamp,Javascript,Firebase,Google Cloud Firestore,Nosql,Timestamp,我正在做一个小测试来进入Firebase。但现在,当我试图访问timestamp对象时,它返回未定义 这是我试图注销日期的一个数组项(配方)的示例,从控制台复制: {author: "xan", created at: n, title: "awesome test"} author: "xan" created at: n nanoseconds: 0 seconds: 1563660000 __proto__: isEqual: ƒ (t) toDate: ƒ () to
{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);
});
const list=document.querySelector('ul');
常量添加配方=(配方)=>{
让时间=recipe.created_at.toDate();
console.log(recipe.created_at);//返回未定义的??
让html=`
${recipe.title}
${time}
`;
list.innerHTML+=html;
}
db.collection('recipes').get().then(快照=>{
snapshot.docs.forEach(doc=>{
addRecipe(doc.data());
});
}).catch(错误=>{
控制台日志(err);
});
为什么
recipe.created\u at
返回undefined?看起来您的数据库字段名为“created at”,单词之间有空格,但您希望它是“created\u at”,单词之间有下划线。确保读取文档的代码也与写入文档的代码匹配。在第一个代码块中,您使用的是在处创建的,作为响应,您在
处检查创建的。尝试直接console.log(recipe)
。我创建了一个新的时间戳,名称中没有空格。这就解决了问题。谢谢(之所以将其命名为“created at”,是因为我在一个示例中看到了它,在代码中通过“created_at”调用它似乎是可行的。)