无法读取属性' typeCast'未定义的mysql

时间:2018-01-29 22:14:44

标签: javascript mysql typescript promise es6-promise

我有代码:

=-=- Fetching repository information =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
00:00.299  REPOSITORY              update default(0 git https://github.com/ocaml/opam)
Processing: [default: git]
Processing: [default: git]
Processing: [default: git]
Processing: [default: git]
Processing: [default: git]
Processing: [default: git]
[default] https://github.com/ocaml/opam updated
00:19.853  REPOSITORY              Updating ~/.opam/repo/compiler-index ...

00:19.853  REPOSITORY              compiler-index
00:19.853  COMPILER                prefixes /Users/al1nk/.opam/repo/default/compilers
00:19.854  FILE(repo-index)        Wrote ~/.opam/repo/compiler-index in 0.000s
00:19.854  REPOSITORY              Updating ~/.opam/compilers/ ...

00:19.854  REPOSITORY              global-index: { system:eff11570a021d31b9aa061882afdd2a8 }
00:19.854  REPOSITORY              repo-index  : {}
00:19.854  REPOSITORY              updated-compilers: {}
00:19.854  REPOSITORY              new-compilers    : {}
00:19.854  REPOSITORY              deleted-compilers: {}
00:19.854  REPOSITORY              Updating ~/.opam/repo/package-index ...

00:19.854  REPOSITORY              package-index
00:19.854  PACKAGE                 prefixes /Users/al1nk/.opam/repo/default/packages
00:19.857  FILE(repo-index)        Wrote ~/.opam/repo/package-index in 0.000s
00:19.857  REPOSITORY              Updating ~/.opam/packages/ ...

00:19.857  REPOSITORY              new-packages     : {}
00:19.857  REPOSITORY              updated-packages : {}
00:19.857  REPOSITORY              changed-packages : {}
00:19.857  REPOSITORY              missing-installed: {}
00:19.857  REPOSITORY              deleted-packages: {}
00:19.857  REPOSITORY              packages-to-reinstall: {}
00:19.857  STATE                   add-to-reinstall all:true packages:{}
00:19.857  CLIENT                  updating package state
00:19.857  STATE                   install_compiler switch=system compiler=system
00:19.857  SYSTEM                  mkdir /Users/al1nk/.opam/system/lib
00:19.857  SYSTEM                  mkdir /Users/al1nk/.opam/system/lib/stublibs
00:19.858  SYSTEM                  mkdir /Users/al1nk/.opam/system/lib/toplevel
00:19.858  SYSTEM                  mkdir /Users/al1nk/.opam/system/build
00:19.858  SYSTEM                  mkdir /Users/al1nk/.opam/system/bin
00:19.858  SYSTEM                  mkdir /Users/al1nk/.opam/system/sbin
00:19.858  SYSTEM                  mkdir /Users/al1nk/.opam/system/doc
00:19.859  SYSTEM                  mkdir /Users/al1nk/.opam/system/man
00:19.859  SYSTEM                  mkdir /Users/al1nk/.opam/system/install
00:19.859  SYSTEM                  mkdir /Users/al1nk/.opam/system/man/man1
00:19.859  SYSTEM                  mkdir /Users/al1nk/.opam/system/man/man1M
00:19.859  SYSTEM                  mkdir /Users/al1nk/.opam/system/man/man2
00:19.859  SYSTEM                  mkdir /Users/al1nk/.opam/system/man/man3
00:19.859  SYSTEM                  mkdir /Users/al1nk/.opam/system/man/man4
00:19.859  SYSTEM                  mkdir /Users/al1nk/.opam/system/man/man5
00:19.859  SYSTEM                  mkdir /Users/al1nk/.opam/system/man/man6
00:19.860  SYSTEM                  mkdir /Users/al1nk/.opam/system/man/man7
00:19.860  SYSTEM                  mkdir /Users/al1nk/.opam/system/man/man9
00:19.860  STATE                   install_global_config switch=system
00:19.860  FILE(.config)           Wrote ~/.opam/system/config/global-config.config in 0.000s
00:19.861  STATE                   add_switch switch=system compiler=system
00:19.863  CLIENT                  installing compiler packages
00:19.863  STATE                   LOAD-STATE(switch-install-with-packages-2)
00:19.864  STATE                   ROOT      : /Users/al1nk/.opam
00:19.864  STATE                   SWITCH    : system
00:19.864  STATE                   COMPILER  : system
00:19.864  STATE                   COMPILERS : { system }
00:19.864  STATE                   REPOS     : { default }
00:19.864  STATE                   PACKAGES  : {}
00:19.864  STATE                   INSTALLED : {}
00:19.864  STATE                   ROOTS     : {}
00:19.864  STATE                   REINSTALL : {}
00:19.864  STATE                   Creating a cache of metadata in ~/.opam/state.cache ...

00:19.865  STATE                   ~/.opam/state.cache written in 0.000s
00:19.865  STATE                   State switch-install-with-packages-2 loaded in 0.001s
[ERROR] Base package base-threads of compiler system not found! Ignored.
[ERROR] Base package base-bigarray of compiler system not found! Ignored.
[ERROR] Base package base-unix of compiler system not found! Ignored.
00:19.885  SOLVER                  resolve request=install:() remove:() upgrade:(base-threads & base-bigarray & base-unix)
00:19.885  SOLVER                  cudf_versions_map
00:19.885  SOLVER                  Load cudf universe (depopts:false, build:true)
00:19.885  CUDF                    resolve request=install:() remove:() upgrade:(base-bigarray & base-threads & base-unix) criteria:"-count(removed),-notuptodate(request),-sum(request,version-lag),-count(down),-notuptodate(changed),-count(changed),-notuptodate(solution),-sum(solution,version-lag)"
00:19.886  SOLVER                  Load cudf universe (depopts:false, build:true)
00:19.886  SOLVER                  Load cudf universe (depopts:true, build:false)
00:19.886  SOLVER                  Load cudf universe (depopts:true, build:true)
00:19.886  CUDF                    graph_of_actions root_actions={}
[ERROR] Inconsistent set of base compiler packages: {} needed but not included / { base-bigarray, base-threads, base-unix } extra
[ERROR] Initialisation failed
OpamGlobals.Exit(66)

以及将ID传递给import { createConnection } from 'mysql'; import { promisify } from 'util'; export default class Database { static instance: Database; query; db; constructor(user, password, database, host) { Database.instance = this; this.db = createConnection({ user, password, database, host }); this.db.connect(); this.query = promisify(this.db.query); } async getUser(id: number, filter: string = null) { return this.query('SELECT * FROM users WHERE id = ' + id) .then((err, res, fields) => { return res[0]; }); } } 的快速路由。

执行此代码时,我会得到此堆栈。

getUser

如何解决此问题?

2 个答案:

答案 0 :(得分:2)

我找到了同一问题的不同解决方案。我没有用这个测试你的代码,但是查看mysql模块的源代码,在我的情况下this不再正确。将query绑定到db对象会为我修复它。在你的情况下,这将是这样的: this.query = promisify(this.db.query.bind(this.db));

答案 1 :(得分:1)

Promisify(...)返回一个承诺。因此,您应该像await一样使用await Promisify(...)this.query = promisify(...)

解决方案是:

1)从this.db.connect();

中删除constructorDatabase

2)您的getUser类和 export default class Database { static instance: Database; static async getInstance(user?, password?, database?, host?) { // all arguments above are optional if(!Database.instance){ const connection = createConnection({ user, password, database, host }); await connection.connect(); const dbQuery = await promisify(this.promisify(connection.query)); Database.instance = { query: dbQuery } return Database.instance; } return Database.instance; } static getUser(id: number, filter: string = null) { return Database.instance.query('SELECT * FROM users WHERE id = ' + id) .then((err, res, fields) => { return res[0]; }); } } 函数应如下所示:

async getUser(){
    let database = await Database.getInstance(); // or Database.getInstance(user, password ...)  to first initialize and then get the instance.

    return database.getUser(1234, filter);
}

使用:

然后,无论你需要什么:

# some df
ggplot(df,aes(x=x,y=y)) + geom_line() + geom_point() + 
   annotate('text', ...)