提交新交易时,moment.js 中的日期不会更改,仍然保存最后日期,直到刷新服务器

发布于 2025-01-14 17:26:59 字数 532 浏览 4 评论 0原文

我正在使用 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

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唯憾梦倾城 2025-01-21 17:26:59

当使用 moment().toDate() 作为默认值时,默认值将设置为您启动应用程序时的日期和时间并保持不变。您想要做的是指定一个返回当前日期的函数,这将导致该函数在每次插入时执行,从而获取实际的当前时间:

const Schema = new mongoose.Schema({
    transactionTime: {
        type: Date,
        default: () => 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:

const Schema = new mongoose.Schema({
    transactionTime: {
        type: Date,
        default: () => moment().toDate(),
    },
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文