javascript - 为什么 Firebase 时间戳对象返回未定义?

标签 javascript firebase google-cloud-firestore nosql timestamp

我正在做一些测试以进入 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/

相关文章:

javascript - 如何从 firebase 逐个访问元素

javascript - 命名空间对象原型(prototype)

javascript - 如何获取多选框的所有选定值?

javascript - 在客户端(即在 javascript 中)检查 firebase db ref 的正确方法是什么?

firebase - 如何从 Firestore 数据库返回列表作为 Kotlin 中函数的结果?

firebase - 如何更新 firebase 数据库中具有随机生成的 ID/KEY 的值

swift - 获取 Firestore 子集合

javascript - ui-grid分组: ordering by aggregated value

javascript - AngularJS 用函数调用数组

firebase - 如何使用Cloud FireStore查询两个日期范围之间的数据?