0

I have used a class for fetching all data to store in a one object. But the object is undefined after running successfully its seems that data is stored in object(Alloptions).

I have tried but can not find sutiable way of doing

router.get('/:category', (req, res) => {
  let AllOptions;
  let cat4;

  AllOptions = new GetAllOptions(); //show undefined
  let category = req.params.category.replace(/-/g, ' ')
  console.log(AllOptions)
  for (i = 0; i < AllOptions.categories.length; i++) {
    console.log(AllOptions.categories[i].categoryName.replace(/-/g, ' ').toLowerCase())
    console.log(category)
    if (AllOptions.categories[i].categoryName.replace(/-/g, ' ').toLowerCase() == category) {
      cat4 = AllOptions.categories[i].categoryId - 1
    }
  }
})

Class object

class GetAllOptions {
  constructor() {
    this.categories = {};
    this.languages = {};
    this.getJobCategories();
  }
  async getJobCategories() {
    await Category.getAllCategories((err, response) => {

      if (err) {
        //?? how error flash dont know
        return res.json({
          error: ERROR_MSG,
        });
      }
      if (response.length > 0) {
        for (let i = 0; i < response.length; i++) {
          this.categories[response[i].categoryId] =
            response[i];
        }

        for (let i = 0; i < response.length; i++) {
          this.categoriesNames[response[i].categoryName] =
            response[i];
        }

      }
    })
    this.getJobLanguages();
  }

  async getJobLanguages() {
    await Category.getAllLanguages((err, response) => {
      if (err) {
        //?? how error flash dont know
        return res.json({
          error: ERROR_MSG,
        });
      }
      if (response) {
        for (let i = 0; i < response.length; i++) {
          this.languages[response[i].languageId] =
            response[i];
          console.log(this.languages[response[i].languageId])
        }

        for (let i = 0; i < response.length; i++) {
          this.languagesNames[response[i].languageName] =
            response[i];
        }
      }
    })
  }
}

I want to wait until class run completed and fetch all data but here showing undefined

varun agarwal
  • 1,373
  • 4
  • 9
  • 1
    As mentioned [here](https://stackoverflow.com/questions/43431550/async-await-class-constructor), you cannot have async constructors. You could create an init function which makes the async call. – varun agarwal Feb 14 '19 at 06:33
  • 1
    You are using `await` syntax with `callback style function`, I think, just use a style `async/await` or `callback`. – hoangdv Feb 14 '19 at 06:59

1 Answers1

0

you can construct your GetAllOptions object asynchronously :

      constructor (async_param) {
            if (typeof async_param === 'undefined') {
                throw new Error('Cannot be called directly');
            }
        }
        static async build () {
            var async_result = await getJobCategories()();
            return new myClass(async_result);
        }
A.Abdelhak
  • 120
  • 4