首页 > 解决方案 > 简单地在 Vue 方法中解析时间,推送到 Firebase DB

问题描述

我正在一个带有 Firebase DB 的 Vue.js 项目中使用 TipTap 创建一个留言板。我宁愿不必使用 moment.js 或其他库,因为它似乎是多余的。

这是我认为是正确的简单方法解析的代码。如果你问我,漂亮的香草 JS。

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

我首先得到一个 Vue 警告说这个 -

[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;
      }"

然后错误说这个 -

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

标签: javascriptfirebasevue.js

解决方案


您正在尝试将函数保存在数据库中而不是调用它。这样做:

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

而不是传递时间戳函数本身,调用它:)


推荐阅读