Knex选择查询后如何使用数据

时间:2018-05-29 01:55:46

标签: node.js knex.js

使用knex select从表中检索数据时遇到一些麻烦。

我想做这样的事情

function getUserData(userId){
    let user = knex.select('xp','money','rolls','twenty').from('users').where('user_id', userId);
   return user;
}


user = getUserData(userId);
user.xp;   // do something with this value

但是这会输出以下内容(如果我是console.log),但不会输出Select查询中请求的信息,除非我不确定如何检索它:

Builder {
  client:
   Client_MySQL {
 config: { client: 'mysql', connection: [Object] },
 connectionSettings:
  { host: '127.0.0.1',
    user: 'XXXXXXXXXX',
    password: 'XXXXXXXXXX',
    database: 'XXXXXXXXXX' },
 driver:
  { createConnection: [Function: createConnection],
    createPool: [Function: createPool],
    createPoolCluster: [Function: createPoolCluster],
    createQuery: [Function: createQuery],
    escape: [Function: escape],
    escapeId: [Function: escapeId],
    format: [Function: format],
    raw: [Function: raw] },
 pool:
  Pool {
    creator: [Function: create],
    destroyer: [Function: destroy],
    validate: [Function: validate],
    log: [Function],
    acquireTimeoutMillis: 60000,
    createTimeoutMillis: 30000,
    idleTimeoutMillis: 30000,
    reapIntervalMillis: 1000,
    createRetryIntervalMillis: 200,
    propagateCreateError: true,
    min: 2,
    max: 10,
    used: [],
    free: [],
    pendingCreates: [],
    pendingAcquires: [],
    destroyed: false,
    interval: null },
 valueForUndefined:
  Raw {
    client: [Circular],
    sql: 'DEFAULT',
    bindings: undefined,
    _wrappedBefore: undefined,
    _wrappedAfter: undefined,
    _debug: undefined },
 _events:
  { start: [Function],
    query: [Function],
    'query-error': [Function],
    'query-response': [Function] },
 _eventsCount: 4,
 makeKnex: [Function: makeKnex] },
  and: [Circular],
  _single: { table: 'users', only: false },
  _statements:
   [ { grouping: 'columns', value: [Array] },
     { grouping: 'where',
       type: 'whereBasic',
       column: 'user_id',
       operator: '=',
       value: '341007826375802900',
   not: false,
   bool: 'and' } ],
  _method: 'select',
  _debug: undefined,
  _joinFlag: 'inner',
  _boolFlag: 'and',
  _notFlag: false }

我会在这里写一些更多的单词,因为它需要我这样做,因为它主要是代码。我希望这会是足够的话。

1 个答案:

答案 0 :(得分:1)

查询以异步方式运行,因此您需要明确等待它完成。一种方法是使用promises:

forEach

还要确保已建立与数据库的连接。