【问题标题】:Node.js cannot read property 'then' of undefinedNode.js 无法读取未定义的属性“then”
【发布时间】:2017-02-28 09:09:57
【问题描述】:

我尝试使用一个全局函数,我可以在其中使用一些mysql函数,但问题是js说“.then”是未定义的,是什么让我错了,这只是一个语法错误吗?

 static connectWidthCortex(){
  xdevapi.getSession({
        host: 'localhost',
        port: 33060,
        dbUser: 'admin',
        dbPassword: 'xxxx'
    }).then((session)=> {
  return session.getSchema("cortex");
});
};

static createCollection(collname){
    this.connectWidthCortex().then((db)=> {
  console.log("Cortex connected")
  return db.createCollection(collname);
}).catch((err)=> {
  console.log("connection failed")
});
}

感谢帮助:)

【问题讨论】:

  • 我猜connectWidthCortex() 的返回值不是一个承诺。可以查一下吗?

标签: javascript mysql node.js


【解决方案1】:

您正试图在connectWidthCortex 的返回值上调用then

connectWidthCortex 函数没有return 语句,因此它返回undefined

如果你想返回调用getSession 给你的承诺,那么你需要一个return 声明。

return xdevapi.getSession({ …

【讨论】:

    猜你喜欢
    • 2020-04-17
    • 2017-04-21
    • 2019-12-10
    • 2019-08-19
    • 2016-10-23
    • 2014-09-07
    • 2015-09-08
    • 2018-01-12
    相关资源
    最近更新 更多