A
A
Alexander Afanasiev2022-02-02 16:41:44
Node.js
Alexander Afanasiev, 2022-02-02 16:41:44

Why are class fields not initialized?

Hello! There is the following code:

import { Client } from 'service'; 
import Redis from 'ioredis';

import config from './config.js';

class ServiceFactory
{
    #client = null;
    #redis = null;

    #qr = null;
    #ready = false;
    #error = null;

    constructor()
    {
        this.#redis = new Redis(config.redis);
        this.init();
    }

    async init()
    {
        this.#client = new Client(await this.#loadSession());

        this.#client.on('auth_failure', this.#onFail);
        this.#client.on('ready', this.#onReady);
        this.#client.on('disconnected', this.#onFail);
        this.#client.on('qr', this.#onQr);

        this.#client.initialize();
    }

    #onFail(error_msg)
    {
        this.#ready = false;
        this.#error = error_msg;
    }

    #onReady()
    {
        this.#ready = true;
    }

    #onQr(qr_code)
    {
        this.#qr = qr_code;
    }

    async #loadSession()
    {
        const session = await this.#redis.get(config.service.redisKey);
        return JSON.parse(session);
    }

    async #saveSession(session)
    {
        return await this.#redis.set(config.service.redisKey,  JSON.stringify(session));
    }
}

export default ServiceFactory;


An error occurs:
this.#qr = qr_code;
                 ^

TypeError: Cannot write private member #qr to an object whose class did not declare it


It turns out because of the asynchronous init () class fields do not have time to initialize? How can this be fixed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arseniy Oguzov, 2022-02-02
@afal97

this in the handler refers to the Client class , not the ServiceFactory . To fix it, you can do this or make the #onQr method an arrow function
this.#client.on('qr', this.#onQr.bind(this));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question