Skip to content
Advertisement

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.

Advertisement

Answer

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! 🙂

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement