Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
Why are JS class instance properties null after setting?

This is a nodejs / express MVC app. I've got a custom model called MyModel (not really, but for privacy, let's pretend that's the name). The properties of the class are set as null by default:

export class MyModel {

    /* ----- FIELDS ----- */
    protected _id: string = null;
    protected _name: string = null;
    ...

The constructor only sets the name property, all other properties are set by database triggers. So in my save method (which persists to the db), it works kind of like this:

    async save() {
        let client:PoolClient = null;
        try {
            client = await pool.connect();
            let query;
            if (this._id === null) {
                query = {
                    name: 'insertMyModel',
                    text: 'INSERT INTO my_model (name) VALUES ($1::text) RETURNING id',
                    values: [this._name],
                    rowMode: 'array',
                };

            } else {
                ...
            }

            const result = await client.query(query);
            if (this._id === null) {
                this._id = result.rows[0][0];
                ...

The controller, simply (for now) returns MyModel to string, which should show the returned id.

    public async new(req, res) {
        try {
            const mymodel = await MyModel.new(req.params.name);

            res.send(mymodel.toString());
        } catch (error) {
            res.status(400).send(error);
        }
    }

And (thank you @Barmar for noticing) I forgot to include MyModel's new function, so adding that here. This constructs the new MyModel instance and then calls save.

    public static async new(name: string) : Promise<GekModel> {
        if (name === undefined || name === null || (name = name.trim()) === '') {
            name = uniqueNamesGenerator();
        }

        const mymodel = new MyModel(name);
        mymodel.save();
        return mymodel;
    }

Using interactive debugging, I've determined that in the save method, indeed, the db is propertly returning the ID. And I've seen that in the save method, the returned id is assigned to the _id property. However, by the time I get back to the controller's new, the _id property is back to null. boink.

I assume this has something to do with the async nature of it? But, I'm at a loss. Would love any ideas!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Big thanks @Barmar for the suggestion of writing a minimal code reproducible version of this problem. I was able to whittle it down to 24 lines and realized that the problem was in the mymodel.save() line. I was calling save synchronously, when it's an async method. Therefore the toString() method was getting called before the id was set. I added an "await" before that and it's working now.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!