Use mysql data output in different ways in nodejs

Morten Hagh

I have an output of a number of bookings which I have to use in a couple of ways.

I will use it to display a list of current bookings and also a list of past bookings, based on a "dateto" field in a timestamp format

Instead of making multiple queries ( WHERE dateto < currentdate, WHERE dateto > currentdate, etc. ) I just want to use the data I already have, but I am a bit unsure how to approach this.

I have this code.

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() is just a function that sums all booking days in a year-to-date - not important for this particular issue.

I want to take bookings and somehow mapping it to make a new array (pastBookings) with dateto < currentdate

So in my EJS file i can use it with something like this

res.render('profile/bookings/bookings.ejs', {
    currentBookings: bookingsMappedToCurrent,
    pastBookings: bookingsMappedToPast
    moment: moment,
    totalDays: await countBookingDays(bookings)
})
Morten Hagh

Man! It's easy to overcomplicate things

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

Works perfectly fine!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Different ways to use Fragments

Same input but different ways of output different results?

What are different ways of parsing data

NodeJS: Different two ways define views directory

What are the different ways of writing output to console in Perl?

In python,if i use [] for holding the data then the output Is correct otherwise i use {} then the output is different

use one date variable but format it different ways

How to slice multidimensional data in different ways in R

difference between two different ways to query data

Different Ways of attaching data to $scope in Angular

Firebase fetching child data for two different ways

MySQL - When should I use a different table for similar data?

MySQL: Is there a way to select a column and use it in "multiple ways"?

PHP and MySQL query output different

Output MySQL data in Modal

use same component for different routes with different output

Webscraping: output different to original data

Data renders twice with different output

nodejs passport - use same routes in api but return different sets of data based on permissions

NodeJS: zlib.gzipSync gives different plaintext output on different platforms

use nodejs to query mysql database

MySQL varchar timestamp column stored two different ways

mySQL - need help creating a query that references several tables in different ways

Different Ways to use simple_form in Ruby on Rails | What is the difference?

What are the different ways of writing an IIFE? What are their use cases?

Is it possible to use CSSTransitionGroup to animate in two different ways depending on state?

Different ways of splitting numbers into lists and when to use them?

Android different ways to create viewModel object which one to use when?

GRU same configurations but in two different ways produces two different output in tensorflow