Property of my object is returning Undefined

Asked

Viewed 222 times

-1

I have the following function:

async geraQuiz(idEstudante) {
  let idioma = new Array()
  let livrosUnidadesGerarQuiz = new Array()
  const idsClassesEstudante = await this.getIdClassesEstudante(idEstudante) // Aqui obtenho como retorno o array [2,3]

  for(let i=0;i<idsClassesEstudante.length;i++){
   livrosUnidadesGerarQuiz = new Array() // Limpa o array para retirar objetos de iterações anteriores
   idioma.push(await this.getDadosClasseIdiomaEstudante(idsClassesEstudante[i]))
   livrosUnidadesGerarQuiz = await this.getLivrosUnidadesGerarQuiz(idsClassesEstudante[i], idBookClasseEstudante[0], qtdQuizGerar)
   idioma[i].quiz = livrosUnidadesGerarQuiz

   console.log(JSON.stringify(idioma))

   console.log(idioma[i].quiz[0])

   ...

On the console.log idioma, i have the following object array:

[
  {
    "id": 2,
    "code": "ING-NOT-2019",
    "description": "Inglês Noturno 2019",
    "start_date": "2019-12-30T03:00:00.000Z",
    "end_date": "2019-12-31T03:00:00.000Z",
    "period": "Noturno",
    "language": "Inglês",
    "status": false,
    "user_id": 1,
    "created_at": "2019-12-30 10:04:47",
    "updated_at": "2020-01-05 16:08:00",
    "language_substring": "US",
    "quiz": [
      {
        "id": 1,
        "class_id": 2,
        "book_unit_id": 1,
        "book_id": 1,
        "start_date": "2020-01-03T03:00:00.000Z",
        "end_date": "2020-01-15T03:00:00.000Z",
        "book_unit_sequence": 1,
        "status": false,
        "user_id": 1,
        "created_at": "2019-12-27T11:11:21.000Z",
        "updated_at": "2019-12-30T17:54:12.000Z",
        "unit": 1,
        "sequence": 1,
        "description": "UNIT_01_GRAMMAR",
        "qt_question": 5,
        "miniature": null
      },
      {
        "id": 2,
        "class_id": 2,
        "book_unit_id": 2,
        "book_id": 1,
        "start_date": "2020-01-15T03:00:00.000Z",
        "end_date": "2020-01-31T03:00:00.000Z",
        "book_unit_sequence": 2,
        "status": false,
        "user_id": 1,
        "created_at": "2019-12-27T11:11:39.000Z",
        "updated_at": "2019-12-27T11:11:39.000Z",
        "unit": 1,
        "sequence": 2,
        "description": "UNIT_01_VOCABULARY",
        "qt_question": 5,
        "miniature": null
      },
      {
        "id": 3,
        "class_id": 2,
        "book_unit_id": 3,
        "book_id": 1,
        "start_date": "2020-01-31T03:00:00.000Z",
        "end_date": null,
        "book_unit_sequence": 1,
        "status": false,
        "user_id": 1,
        "created_at": "2019-12-27T11:11:46.000Z",
        "updated_at": "2019-12-27T11:11:46.000Z",
        "unit": 2,
        "sequence": 1,
        "description": "UNIT_02_GRAMMAR",
        "qt_question": 5,
        "miniature": null
      }
    ]
  }
]

But when I try to access: console.log(idioma[i].quiz[0]) i get undefined, but my console.log() clearly shows that there is a quiz property within the language at position 0, but I cannot access it because it is undefined. 'Cause this is happening?

I put a return shortly after the console.log(idioma[i].quiz[0]) to check if you are not overwriting something later, but get the same result; and

I put a console.log(i) and get 0 as expected.

  • Try changing language[i]. quiz[0] by language[0]. quiz[i]

  • Undefined, as the question, i has the value 0

  • By the.log console of the language you presented, there is only one object at zero position of the array and this object contains a list of objects in the quiz attribute.

  • yes, and when I try to access it I get Undefined

  • The problem is that obtaining this list is asynchronous

  • but await does not wait for the request to finish to continue the execution of the code?

Show 1 more comment

1 answer

0

Apparently it is some insertion problem in the serialized object that Adonis returns from my query, I was able to solve by converting my language array to a simple javascript object:

idioma.push(JSON.parse(JSON.stringify(await this.getDadosClasseIdiomaEstudante(idsClassesEstudante[i]))))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.