javascript - 简单地在 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;
}
解决方案
您正在尝试将函数保存在数据库中而不是调用它。这样做:
db.ref('thanktank').push({
authorID: this.currentUserId,
text: this.editor.getHTML(),
timestamp: timestamp()
})
而不是传递时间戳函数本身,调用它:)
推荐阅读
- anylogic - 如何从上传的数据库表中将代理注入源块中?
- java - 测试端点定义和所需参数的最简单方法是什么
- mysql - 我正在尝试将数据从 MySQL Workbench 导出到 csv,但它不断将特定列值转换为科学记数法
- javascript - 有没有办法在 React 的 componentDidMount 方法中使用事件对象?
- python - 我可以将 UDP 数据包从我的电脑发送到另一台电脑,但我无法发送 tcp 数据包
- blockchain - 获取 Avalanche C-Chain 合约 Abi 的
- keras - 如何返回从 Keras model.fit_generator 生成的样本?
- javascript - 为什么 !+[]+[]+![] 是“真假”?
- gitlab - 如何使用本地 gitlab-runner 测试两个作业?
- python-3.x - 序列与可变序列