首页 > 解决方案 > 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)
})

标签: javascriptarraysnode.jsmappingejs

解决方案


男人!很容易把事情复杂化

let currentBookings = bookings.filter(booking => booking.b_dateto > timeNow)
let pastBookings = bookings.filter(booking => booking.b_dateto < timeNow)

工作得很好!


推荐阅读