Javascript 属性则不会';I don’我不存在于类型void上,尽管我返回了一个承诺
我有一个函数,可以在前端获取机器人的库存Javascript 属性则不会';I don’我不存在于类型void上,尽管我返回了一个承诺,javascript,angular,typescript,Javascript,Angular,Typescript,我有一个函数,可以在前端获取机器人的库存 getBotInventory() { this.socket.emit('get bot inv'); this.socket.on('bot inv', (botInventory) => { return new Promise((resolve, reject) => { if (botInventory.error) { return reject(botInven
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);
});
});
}
正如你所看到的,它回报了一个承诺
当我试着叫它
getBotInventory() {
this.userService.getBotInventory().then(botInv..)
}
我要走了
错误TS2339:类型“void”上不存在属性“then”
显然,它返回一个
承诺
。有人能指出问题出在哪里吗?这对你来说可能是显而易见的,但对我来说不是
看看你的函数,清除了回调
getBotInventory() {
this.socket.emit('get bot inv');
this.socket.on('bot inv', (botInventory) => {...});
}
我没有看到任何返回声明 在回拨地狱的世界里迷路了。谢谢没问题!如果你需要关于如何处理这件事的帮助,尽管问吧getBotInventory(){返回新承诺((解析,拒绝)=>{this.socket.emit('get bot inv');this.socket.on('bot inv'),(botInventory)=>{if(botInventory.error){返回拒绝(botInventory.error);}this.botInventory=botInventory;解析(botInventory);}}“让它工作起来了:)