0

I have a simple class Date with a few functions. One of them is SetMonthAndYear.

I export this class with module.export to make it accessible for other classes:

class MyDateClass {
    currentMonth = 0;
    currentYear = 0;

    async SetMonthAndYear(timeStamp){
        var date = new Date(timeStamp);
        if(date.getFullYear() != this.currentYear || (date.getMonth() + 1) != this.currentMonth){
            this.currentYear = new Date(Date.now()).getFullYear();
            this.currentMonth = new Date(Date.now()).getMonth() + 1; // getMonth() gives 1 month earlier for some reason.
            console.log("New month and year is set: " + this.currentMonth + " - " + this.currentYear)
            return true;
        }
        return false;
    }
}

module.exports = new MyDateClass();

In an other class I request this function to check if the month and year need to be updated like so:

var dateClass = require(./MyDateClass.js);

class OtherClass
{
    var currentTimeStamp = ~some timestamp~;
    
    async main(){
       if (await dateClass.SetMonthAndYear(currentTimeStamp))
       {
           console.Log("The month and year are modified");
       }
       console.Log("Done.");
    }
    
    main();
}

Now the weird part: The first run this code sets the month and year and gives this as output (which I expect):

New month and year is set: 1 - 2025
The month and year are modified
Done.

The second run this is the output:

The month and year are modified
Done.

So in the function the if statement is seen as false, but the function seems to be returning true while I expect false as a return value?

I'm new with Nodejs module exports and suspect I'm doing something wrong here, but can't figure out what exactly?

5
  • Why did you make the method async? Also don't export class instances. Commented Jan 8 at 10:27
  • 1
    You have a syntax error in your class OtherClass, its body contains raw statements. Please provide a minimal reproducible example. Are you sure your actual code contains the await? Commented Jan 8 at 10:28
  • 1
    Don't declare a class named Date, it shadows the global Date object. You seem to be mixing them up yourself already. Commented Jan 8 at 10:30
  • @Bergi The name Date is a place holder to keep the example small, clean and simple. the other class is simplified code from my project and correct. Yes it is missing some parts what it is actually doing, but this is the part that isn't working Like I expect. I made the method async because most of my project is. I like to handle the async part myself te be sure to be awaited or parrallel when able. Commented Jan 8 at 10:55
  • @bergi added the main function to make it reproducible. Commented Jan 8 at 11:08

1 Answer 1

1

Since i don't have enought rep to comment Im writing an answer.

  • You might want to export the class and not the instance of the class. That might be causing an issue - the currentMonth and currentYear won't get reset.
  • You would also want to avoid creating conflicting class names like Date
  // when exporting
  module.exports = myDateClass


  // when importing
  const Date = require('./myDateClass')

  // when calling method - instantiate new class
  const dateClass = new DateClass()
  if (await dateClass.SetMonthAndYear(currentTimeStamp)) {
      console.log("The month and year are modified")
  }
  console.log("Done.")

Alternativley you could reset currentMonth and currentYear on every class method call

Sign up to request clarification or add additional context in comments.

2 Comments

This looks promising. Just a small question: if I export the class instance I seem to be able to directly use it across multiple classes like a Singleton. How do I fix this with your suggestion?
My suggestion said not to do that. Re-looking & re-running the edited code without the conflicting names. I think you should be able to pull off exporting as instance of class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.