express - 如何每天启用一次 Express.post 路由
问题描述
我有一个 express.js 应用程序,允许用户发布法案。我想将这些帖子限制为每天一个。从服务器端开始,我想仅在当前用户在同一天没有发布任何行为的情况下更改发布路线。在当前路线(如下所示)中,我试图将上午 00:00 的当前日期与用户上一幕的 created_at 日期进行比较,如果 created_at >= start_of_current_date,则不会发生帖子。但是,created_at 被记录,但在条件中返回 undefined。
这是我的路线:
// POST new Act route
router.post('/', auth.required, async (req, res, next) => {
const now = new Date();
// return last created Act
result = await Act
.query()
.where('users_id', req.user.id)
.orderBy('created_at', 'desc')
.limit(1);
console.log('this is the last act of user ', req.user.id, ': ', result);
console.log('this is the created_at last act of user ', req.user.id, ': ', result.created_at);
console.log('this is the start of today ', req.user.id, ': ', now.setHours(0,0,0,0));
// conditional to disable if last Act.created_at == Today
if(result.created_at >= now.setHours(0,0,0,0)) {
return Act.query()
.insert({
deed: req.body.act.deed,
users_id: req.body.act.userId
})
.then( function () {
res.json({ success: true, message: 'ok' }); // respond back to request
})
} else {
res.json ({
"message": "This user already acted today."
});
}
});
这是控制台中的结果,包括我的控制台日志:
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
Running on localhost:8000
this is the last act of user 1 : [ Act {
id: 6,
deed: 'Writing',
users_id: 1,
created_at: 2019-09-23T20:04:28.393Z,
updated_at: 2019-09-23T20:04:28.393Z } ]
this is the created_at last act of user 1 : undefined
this is the start of today 1 : 1569902400000
更新:结果返回一个数组,所以使用结果 [0],我可以获得一个值。h/e,需要转换输出以将其与当前日期进行比较。这是当前的输出:
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
Running on localhost:8000
this is the last act of user 1 : [ Act {
id: 6,
deed: 'Writing',
users_id: 1,
created_at: 2019-09-23T20:04:28.393Z,
updated_at: 2019-09-23T20:04:28.393Z } ]
this is the date of the last act of user 1 : 2019-09-23T20:04:28.393Z
this is the start of today 1 : 1569902400000
解决方案
这有效,使用 getTime() 助手:
// POST new act route
router.post('/', auth.required, async (req, res, next) => {
const now = new Date();
// return last created Act
result = await Act
.query()
.where('users_id', req.user.id)
.orderBy('created_at', 'desc')
.limit(1);
console.log('this is the last act of user ', req.user.id, ': ', result);
console.log('this is the date of the last act of user ', req.user.id, ': ', result[0].created_at.getTime());
console.log('this is the start of today ', req.user.id, ': ', now.setHours(0,0,0,0));
// conditional to disable if last Act.created_at == Today
if(result[0].created_at.getTime() <= now.setHours(0,0,0,0)) {
return Act.query()
.insert({
deed: req.body.act.deed,
users_id: req.body.act.userId
})
.then( function () {
res.json({ success: true, message: 'ok' }); // respond back to request
})
} else {
res.json ({
"message": "This user already acted today."
});
}
});
推荐阅读
- xamarin - 更新 UI 然后添加延迟
- node.js - NodeJS:将变量传递给嵌套函数
- c# - 使用 C# 依赖注入的工厂类的最佳实践?
- php - Wordpress - 当其他用户喜欢他们的帖子时向用户发送电子邮件
- python - python Set 方法已订购但不起作用为什么?
- java - 超过“9999”时将数字字符串值重置为“1”
- android - 如何在 Android 中使用 Retrofit 获取 XML 响应
- angular - Ag-grid 保存/恢复过滤器状态不起作用
- vue.js - Vue-router:加载命名视图而不重新加载当前活动视图
- javascript - 如何对 innerHTML 执行操作