Cinder
Cinder

Reputation: 349

Why does Firebase timestamp object return undefined?

I'm doing a little test thing to get into Firebase. But now when I'm trying to access my timestamp object, it returns undefined.

This is an example for one of the array items (recipe) I'm trying to log out the date for, copied from the console:

{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

And here is my code:

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

Why does recipe.created_at return undefined?

Upvotes: 0

Views: 628

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317878

It looks like your database field is called "created at" with a space between the words, but you're expecting it to be "created_at" with an underscore between the words. Make sure that the code that reads the document also matches the code that writes the document.

Upvotes: 1

Related Questions