提交新交易时,moment.js 中的日期不会更改,仍然保存最后日期,直到刷新服务器
我正在使用 moment.js
格式化日期并将其保存在 DB
Schema 代码
const Schema = new mongoose.Schema({
transactionTime: {
type: Date,
default: moment().toDate(),
},
前端代码
<td>{moment(transaction.transactionTime).format('MMMM Do YYYY, h:mm:ss a')}</td>
中,但是当我提交交易时,日期并未实时更新,我发送它时我必须刷新服务器才能更新日期
I am using moment.js
to format date and save it in DB
Schema code
const Schema = new mongoose.Schema({
transactionTime: {
type: Date,
default: moment().toDate(),
},
front code
<td>{moment(transaction.transactionTime).format('MMMM Do YYYY, h:mm:ss a')}</td>
but when I submit a transaction the date is not updated for real-time I send it I must refresh the server to update the date
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当使用
moment().toDate()
作为默认值时,默认值将设置为您启动应用程序时的日期和时间并保持不变。您想要做的是指定一个返回当前日期的函数,这将导致该函数在每次插入时执行,从而获取实际的当前时间:When using
moment().toDate()
as the default, the default value is set to the date and time when you started the application and stays constant. What you want to do is to rather specify a function that returns the current date, which would result in the function being executed at each insertion, thus getting the actual current time: