繁体   English   中英

属性未定义但 console.log() 打印属性

[英]Property undefined but the console.log() prints the property

我有这个功能:

async geraQuiz(idEstudante) {
  let idioma = new Array()
  let livrosUnidadesGerarQuiz = new Array()
  const idsClassesEstudante = await this.getIdClassesEstudante(idEstudante) // Here i have this array return: [2,3]

  for(let i=0;i<idsClassesEstudante.length;i++){
   livrosUnidadesGerarQuiz = new Array() // Clear the array
   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])

   ...

console.log(idioma)我有这个对象数组:

[
  {
    "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
      }
    ]
  }
]

但是,当我尝试使用以下方法访问该属性时: console.log(idioma[i].quiz[0]我收到undefined ,但是console.log()显示我有这个属性quiz并且不是undefined ,为什么会发生这种情况?

我在console.log(idioma[i].quiz[0])后面放了一个 return 以防止它不是订阅,但我有相同的结果;

我放了一个console.log(i)并且我的值为 0

@编辑

没有字符串化的console.log(idioma)

[
  Class {
    __setters__: [
      '$attributes',
      '$persisted',
      'primaryKeyValue',
      '$originalAttributes',
      '$relations',
      '$sideLoaded',
      '$parent',
      '$frozen',
      '$visible',
      '$hidden'
    ],
    '$attributes': {
      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-30T13:04:47.000Z,
      updated_at: 2020-01-05T19:08:00.000Z,
      language_substring: 'US',
      quiz: [Array]
    },
    '$persisted': true,
    '$originalAttributes': {
      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-30T13:04:47.000Z,
      updated_at: 2020-01-05T19:08:00.000Z,
      language_substring: 'US'
    },
    '$relations': {},
    '$sideLoaded': {},
    '$parent': null,
    '$frozen': false,
    '$visible': undefined,
    '$hidden': undefined
  }
]

我调查了整个 Adonisjs 框架源代码,并在BaseModel类定义中找到了这一点 它返回一个proxy ,并且在代理内部,他可以捕获属性的getset ,并执行许多您在使用框架时不一定需要知道的事情。

我的建议是避免这种情况:

idioma[i].quiz = livrosUnidadesGerarQuiz

这样你就会触发 BaseModel 的设置陷阱并做你不需要的事情。

尝试将事物分开保存,如下所示:

const idiomas = [];

...

const quiz = await this.getLivrosUnidadesGerarQuiz(idsClassesEstudante[i], idBookClasseEstudante[0], qtdQuizGerar);
const idioma = await this.getDadosClasseIdiomaEstudante(idsClassesEstudante[i])'
idiomas.push({ quiz, idioma });

...

这样您就不会遇到与之相关的访问器或序列化问题,并且您知道idiomas[i].idioma是模型类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM