Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Build a function that returns events that are currently happening in the present within a selected time frame in NodeJs

I am fairly new in NodeJS, and I am trying to build an event management system where the user can see events that are happening now, upcoming events and past events. I have built a model called events which has attributes startDate and endDate which a user selects upon creation of an event. I need to list events that are happening in the present, this is the function I have built so far, however, it doesn’t work and I am pretty sure there is some mistakes in the syntax too. I would really appreciate anyone’s help on this 🙂

exports.listPresent = async (req, res) => {
    try {

        const event = await Event.find({})
    
        const eventPresent = [];

        for (let i = 0; i < event.length; i++) {


            var dateArr = event[i].startDate.toString().split("-");
            var dateArr2 = event[i].endDate.toString().split("-");
            var inputDate = new Date('"' + dateArr[2] + "-" + dateArr[1] + "-" + dateArr[0] + '"').setHours(0, 0, 0, 0);
  
            var inputDate2 = new Date('"' + dateArr2[2] + "-" + dateArr2[1] + "-" + dateArr2[0] + '"').setHours(0, 0, 0, 0);
            
            var toDay = new Date().setHours(0, 0, 0, 0);
            
            if (event[i].startDate != null && event[i].startDate != '' && event[i].endDate !=null && event[i].endDate != '') {

                if(inputDate == toDay || (inputDate >= toDay && inputDate <= inputDate2)){  
                     eventPresent.push(i);
                }
            }
            return eventPresent;;
          }
 
    
    } catch (error) {
    res.status(500).json( { error: error.message });
    }
};

I tried writing a function (the one above) but I am new at JavaScript and I am a bit lost on how to make this work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The logic is correct in this one, you just need to replace "return eventPresent" to res.status(200).json(eventPresent) – and put this outside the for loop, and also eventPresent.push(i) with eventPresent.push(event[i]), as you are only pushing the index into the array, and not the actual element as such:

    exports.listPresent = async (req, res) => {
    try {

        const event = await Event.find({})
    
        const eventPresent = [];

        for (let i = 0; i < event.length; i++) {


            var dateArr = event[i].startDate.toString().split("-");
            var dateArr2 = event[i].endDate.toString().split("-");
            var inputDate = new Date('"' + dateArr[2] + "-" + dateArr[1] + "-" + dateArr[0] + '"').setHours(0, 0, 0, 0);
  
            var inputDate2 = new Date('"' + dateArr2[2] + "-" + dateArr2[1] + "-" + dateArr2[0] + '"').setHours(0, 0, 0, 0);
            
            var toDay = new Date().setHours(0, 0, 0, 0);
            
            if (event[i].startDate != null && event[i].startDate != '' && event[i].endDate !=null && event[i].endDate != '') {

                if(inputDate == toDay || (inputDate >= toDay && inputDate <= inputDate2)){  
                     eventPresent.push(event[i]);
                }
            }
       
          }

 res.status(200).json(eventPresent);
 
    
    } catch (error) {
    res.status(500).json( { error: error.message });
    }
};

Hope this helps! 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading