我如何在adonis / node.js中的模型返回查询中进行for?

时间:2019-11-19 17:31:10

标签: node.js adonis.js

我有这个查询,是我返回一系列问题:

 const questions = await Question
             .query()
             .all()
console.log(questions)  

但是当我在疑问中创建console.log时,我获得了非常多的信息,并且无法找到对象关系的数组。 这就是为什么显示我的console.log()

QueryBuilder {
  Model:
   { [Function: BookUnitQuestion]
     '$booted': true,
     '$hooks': { before: [Hooks], after: [Hooks] },
     '$queryListeners': [],
     '$globalScopes': GlobalScopes { scopes: [] },
     QueryBuilder: null },
  connectionString: '',
  db:
   Database {
     connectionClient: 'pg',
     knex:
      { [Function: knex]
        Promise: [Function],
        queryBuilder: [Function: queryBuilder],
        raw: [Function: raw],
        batchInsert: [Function: batchInsert],
        transaction: [Function: transaction],
        initialize: [Function: initialize],
        destroy: [Function: destroy],
        ref: [Function: ref],
        _events: [Object: null prototype] {},
        _eventsCount: 0,
        _maxListeners: undefined,
        setMaxListeners: [Function: setMaxListeners],
        getMaxListeners: [Function: getMaxListeners],
        emit: [Function: emit],
        addListener: [Function: addListener],
        on: [Function: addListener],
        prependListener: [Function: prependListener],
        once: [Function: once],
        prependOnceListener: [Function: prependOnceListener],
        removeListener: [Function: removeListener],
        off: [Function: removeListener],
        removeAllListeners: [Function: removeAllListeners],
        listeners: [Function: listeners],
        rawListeners: [Function: rawListeners],
        listenerCount: [Function: listenerCount],
        eventNames: [Function: eventNames],
        with: [Function],
        select: [Function],
        as: [Function],
        columns: [Function],
        column: [Function],
        from: [Function],
        fromJS: [Function],
        into: [Function],
        withSchema: [Function],
        table: [Function],
        distinct: [Function],
        join: [Function],
        joinRaw: [Function],
        innerJoin: [Function],
        leftJoin: [Function],
        leftOuterJoin: [Function],
        rightJoin: [Function],
        rightOuterJoin: [Function],
        outerJoin: [Function],
        fullOuterJoin: [Function],
        crossJoin: [Function],
        where: [Function],
        andWhere: [Function],
        orWhere: [Function],
        whereNot: [Function],
        orWhereNot: [Function],
        whereRaw: [Function],
        whereWrapped: [Function],
        havingWrapped: [Function],
        orWhereRaw: [Function],
        whereExists: [Function],
        orWhereExists: [Function],
        whereNotExists: [Function],
        orWhereNotExists: [Function],
        whereIn: [Function],
        orWhereIn: [Function],
        whereNotIn: [Function],
        orWhereNotIn: [Function],
        whereNull: [Function],
        orWhereNull: [Function],
        whereNotNull: [Function],
        orWhereNotNull: [Function],
        whereBetween: [Function],
        whereNotBetween: [Function],
        andWhereBetween: [Function],
        andWhereNotBetween: [Function],
        orWhereBetween: [Function],
        orWhereNotBetween: [Function],
        groupBy: [Function],
        groupByRaw: [Function],
        orderBy: [Function],
        orderByRaw: [Function],
        union: [Function],
        unionAll: [Function],
        having: [Function],
        havingRaw: [Function],
        orHaving: [Function],
        orHavingRaw: [Function],
        offset: [Function],
        limit: [Function],
        count: [Function],
        countDistinct: [Function],
        min: [Function],
        max: [Function],
        sum: [Function],
        sumDistinct: [Function],
        avg: [Function],
        avgDistinct: [Function],
        increment: [Function],
        decrement: [Function],
        first: [Function],
        debug: [Function],
        pluck: [Function],
        clearSelect: [Function],
        clearWhere: [Function],
        clearOrder: [Function],
        insert: [Function],
        update: [Function],
        returning: [Function],
        del: [Function],
        delete: [Function],
        truncate: [Function],
        transacting: [Function],
        connection: [Function],
        client: [Client_PG] },
     _globalTrx: null },
  query:
   Builder {
     client:
      Client_PG {
        config: [Object],
        logger: [Logger],
        connectionSettings: [Object],
        driver: [PG],
        pool: [Pool],
        valueForUndefined: [Raw],
        _events: [Object],
        _eventsCount: 4,
        makeKnex: [Function: makeKnex],
        version: '12.0,' },
     and: [Circular],
     _single: { table: 'book_unit_question', only: false },
     _statements: [ [Object] ],
     _method: 'select',
     _debug: false,
     _joinFlag: 'inner',
     _boolFlag: 'and',
     _notFlag: false,
     subQuery: [Function],
     _events: [Object: null prototype] { query: [Function] },
     _eventsCount: 1 },
  _eagerLoads: { book_unit: [Function] },
  _sideLoaded: [],
  _visibleFields: undefined,
  _hiddenFields: undefined,
  _withCountCounter: -1,
  scopesIterator: ScopeIterator { _scopes: [], _ignoreList: [] } }

这在我的浏览器中显示,我需要操纵这些数据。

我如何提出有问题的申请?

我需要类似for(let i = 0; i

0: {id: 5, book_unit_id: 5, question_form: "Texto", option_form: "Texto", type_answer: "Aberta",…}
1: {id: 7, book_unit_id: 5, question_form: "Texto", option_form: "Texto", type_answer: "Aberta",…}
2: {id: 8, book_unit_id: 6, question_form: "Texto", option_form: "Texto", type_answer: "Aberta",…}

1 个答案:

答案 0 :(得分:1)

当您在console中显示时,您会使用toJSON()这样在json中进行转换

const questionsjson= questions.toJSON()
console.log(questionsjson)