skip to Main Content

I have this Config.ts file that contains some basic numbers
when initializing app it can read values from variables but later I cant read this variables because of "TypeError: Cannot read properties of undefined (reading ‘initLevrage’)
at getTimeName (config.ts:38:22)"
please Help

import { log } from 'console';
import { ITimeFrameTypes } from '../tbBot/bot.interface';

export class Configs {
  private initMargin = 1;
  private initLevrage = [
    100, 90, 80, 70, 60, 50, 40, 30, 20, 15, 13, 11, 10, 8, 6, 5, 4, 3,
  ];
  private timeFrames: ITimeFrameTypes[] = [
    '1m',
    '5m',
    '15m',
    '1h',
    '4h',
    '1d',
    '1M',
  ];
  checkFrameType(name: ITimeFrameTypes) {
    let t: string = name;
    if (name == '1M') {
      t = '1mo';
    }
    return t;
  }
  getMarginInit() {
    return this.initMargin;
  }
  setMarginInit(n: number) {
    this.initMargin = n;
  }
  getLevrages() {
    return this.initLevrage.map(x=>x);
  }
  getTimes() {
    return this.timeFrames.map(x=>x);
  }
  getTimeName(i: number) {
    log( typeof this.initLevrage);
    let t = this.timeFrames[i];
    return this.checkFrameType(t);
  }
  getTimeIndex(name: ITimeFrameTypes) {
    let t = this.timeFrames.indexOf(name);
    return t;
  }
}

it Logs iniLevrage Values fist but cant find it later

I have tried to move these vaiables out of class scope , then assign them to variables but no diffrence

2

Answers


  1. Chosen as BEST ANSWER

    I found the problem I was calling the function like this: let frame = this.config.getTimeName; then calling frame : frame(i) deleting frame and using this.config.getTimeName(i) , the problem was Gone.

    thanks


  2. I think the issue is likely with how you’re invoking your methods – check out this TS playground I set up

    I think for a quick fix, you can change your methods from

      getTimeName(i: number) {
        log( typeof this.initLevrage);
        let t = this.timeFrames[i];
        return this.checkFrameType(t);
      }
    

    to

      getTimeName = (i: number) => {
        log( typeof this.initLevrage);
        let t = this.timeFrames[i];
        return this.checkFrameType(t);
      }
    

    The latter anonymous function will let you access this from any context you’d like

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search