Parse time simply within Vue method pushing to Firebase DB

Tags: , ,



I am creating a message board using TipTap in a Vue.js project with a Firebase DB. I would prefer not to have to use moment.js or another library as it seems superfluous.

This is the code for my simple method parse which I feel like is correct. Pretty vanilla JS if you ask me.

methods: {
    pushContent() {

      var timestamp = function() {
        let d = new Date(),
            year = d.getYear(),
            day = d.getDay(),
            month = d.getMonth(),

        today = month + "/" + day + "/" + year;

        return today;
      }


      db.ref('thanktank').push({
        authorID: this.currentUserId,
        text: this.editor.getHTML(),
        timestamp: timestamp
      })
      this.editor.clearContent();
    },
  },

I first get a Vue warning saying this –

[Vue warn]: Error in v-on handler: "Error: Reference.push failed: first argument contains a function in property 'thanktank.timestamp' with contents = function timestamp() {
        var d = new Date(),
            year = d.getYear(),
            day = d.getDay(),
            month = d.getMonth(),
            today = month + "/" + day + "/" + year;
        return today;
      }"

And then the error saying this –

Reference.push failed: first argument contains a function in property 'thanktank.timestamp' with contents = function timestamp() {
        var d = new Date(),
            year = d.getYear(),
            day = d.getDay(),
            month = d.getMonth(),
            today = month + "/" + day + "/" + year;
        return today;
      }

Answer

you are trying to save a function in the database instead of calling it. Do like this:

db.ref('thanktank').push({
    authorID: this.currentUserId,
    text: this.editor.getHTML(),
    timestamp: timestamp()
  })

instead of passing the timestamp function itself, call it 🙂



Source: stackoverflow