javascript - nodejs中不同方式使用mysql数据输出
问题描述
我有一些预订的输出,我必须以几种方式使用它们。
我将使用它来显示当前预订列表以及过去预订列表,基于时间戳格式的“dateto”字段
而不是进行多个查询(WHERE dateto < currentdate, WHERE dateto > currentdate 等),我只想使用我已经拥有的数据,但我有点不确定如何处理这个问题。
我有这个代码。
let list = (req, res, next) => {
fetchAll(req.params.hid).then((bookings) => {
if (bookings.length > 0) {
(async () => {
res.render('profile/bookings/bookings.ejs', {
bookings: bookings,
moment: moment,
totalDays: await countBookingDays(bookings)
})
})()
} else {
res.send('No result')
}
}).catch((error) => {
res.status(500).send(error)
})
}
countBookingDays()
只是一个汇总年初至今所有预订天数的函数 - 对于这个特定问题并不重要。
我想采取bookings
并以某种方式映射它以创建一个带有 dateto < currentdate 的新数组(pastBookings)
所以在我的 EJS 文件中,我可以将它与这样的东西一起使用
res.render('profile/bookings/bookings.ejs', {
currentBookings: bookingsMappedToCurrent,
pastBookings: bookingsMappedToPast
moment: moment,
totalDays: await countBookingDays(bookings)
})
解决方案
男人!很容易把事情复杂化
let currentBookings = bookings.filter(booking => booking.b_dateto > timeNow)
let pastBookings = bookings.filter(booking => booking.b_dateto < timeNow)
工作得很好!
推荐阅读
- reactjs - 错误:无法加载更漂亮的插件:找不到模块“eslint-plugin-prettier”
- php - PHP exec() 函数在后台执行的两个 linux 命令
- javascript - 打开移动应用程序的 Javascript 也打开后备 URL
- javascript - 未捕获的语法错误;意外的令牌 <
- c# - 无法为 EntityFrameworkCore 多对一关系创建数据播种对象
- java - 处理来自 Google Play 控制台的崩溃/异常,而我的应用程序源代码中没有堆栈跟踪
- angular - 在 Angular 中动态设置宿主元素样式的两种方法之间的区别
- java - 如何在 CriteriaQuery 中实现 sum
- ios - 提交适用于 iOS 13(预发布)的应用程序流程
- javascript - 在 Vue.js 和 Vuetify 中发射返回错误