【问题标题】:Property then doesn't exist on type void, although I am returning a Promise然后属性在类型 void 上不存在,尽管我返回一个 Promise
【发布时间】:2018-08-21 17:24:32
【问题描述】:

我有一个在前端获取机器人库存的函数

  getBotInventory() {
    this.socket.emit('get bot inv');
    this.socket.on('bot inv', (botInventory) => {
      return new Promise((resolve, reject) => {
        if (botInventory.error) {
          return reject(botInventory.error);
        }
        this.botInventory = botInventory;
        resolve(botInventory);
      });

    });
  }

如你所见,它返回一个 Promise。

当我尝试调用它时

getBotInventory() {
    this.userService.getBotInventory().then(botInv..)
  }

我来了

错误 TS2339:类型“void”上不存在属性“then”。

显然它返回一个Promise。谁能指出问题出在哪里?

【问题讨论】:

    标签: javascript angular typescript


    【解决方案1】:

    这对你来说可能很明显,但对我来说不是。

    查看你的函数,清除回调。

    getBotInventory() {
      this.socket.emit('get bot inv');
      this.socket.on('bot inv', (botInventory) => {...});
    }
    

    我没有看到任何返回声明!

    【讨论】:

    • 迷失在回调地狱的世界中。谢谢!
    • 没问题!如果您需要有关如何处理的帮助,请随时询问!
    • ``` getBotInventory() { return new Promise((resolve, reject) => { this.socket.emit('get bot inv'); this.socket.on('bot inv' , (botInventory) => { if (botInventory.error) { return reject(botInventory.error); } this.botInventory = botInventory; resolve(botInventory); }); }); }```让它工作了:)
    猜你喜欢
    • 2016-12-31
    • 2019-02-04
    • 2018-04-24
    • 2023-03-08
    • 2019-07-23
    • 2019-04-11
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多