如何正确使用.catch()?无法在某些代码中使用它

问题描述 投票:0回答:1

当我运行计划代码以发布消息时,每次发送消息时,我都会收到此错误消息:

未处理的承诺被拒绝。引发此错误的原因可能是抛出了一个没有catch块的异步函数,或者是拒绝了一个.catch()无法处理的承诺。 (拒绝ID:3)

下面的代码在开始时不使用try {,但是即使这样,我也得到了相同的错误消息..

'use strict';

const Telegram = require('telegram-node-bot');
var schedule = require('node-schedule');

class AutoMsgController extends Telegram.TelegramBaseController {
    AutoMsgHandler(scope) {
        console.log(scope);
        if (statement) {
            const button = {'inline_keyboard': [
                [{ text: 'BUTTON1', url: 'URL1' }],
                [{ text: 'BUTTON2', url: 'URL2' }]
            ]}
            const parsedbutton = JSON.stringify(button);
            console.log(parsedbutton);
            scope.api.sendMessage(scope.message._chat._id, '*ACTIVATED!*', { 'parse_mode' : 'Markdown'});
            var j = schedule.scheduleJob('*/5 * * * * *', function(){ //5 Seconds
            scope.api.sendMessage(scope.message._chat._id, 'My message'
            ).then (
            function (e) {
                console.log ('Could not send automsg', e);
                throw e;
              }
            );
    }).catch((err) => {
        console.log('Api call error:', err.message)
      });
    }
}
    get routes() {
        return {
            'AutoMsgCommand': 'AutoMsgHandler'
        };
    }
}

module.exports = AutoMsgController;

如果我尝试使用try {即时通讯无法完成,则说:finally expected

'use strict';

const Telegram = require('telegram-node-bot');
var schedule = require('node-schedule');

class AutoMsgController extends Telegram.TelegramBaseController {
    AutoMsgHandler(scope) {
        console.log(scope);
        if (statement) {
            try {
            const button = {'inline_keyboard': [
                [{ text: 'BUTTON1', url: 'URL1' }],
                [{ text: 'BUTTON2', url: 'URL2' }]
            ]}
            const parsedbutton = JSON.stringify(button);
            console.log(parsedbutton);
            scope.api.sendMessage(scope.message._chat._id, '*ACTIVATED!*', { 'parse_mode' : 'Markdown'});
            var j = schedule.scheduleJob('*/5 * * * * *', function(){ //5 Seconds
            scope.api.sendMessage(scope.message._chat._id, 'My message'
            ).then (
            function (e) {
                console.log ('Could not send automsg', e);
                throw e;
              }
            );
    }).catch((err) => {
        console.log('Api call error:', err.message)
      });
    }
  } //<- here is the problem `finally expected`
}
    get routes() {
        return {
            'AutoMsgCommand': 'AutoMsgHandler'
        };
    }
}

module.exports = AutoMsgController;

无法解决...从其他帖子中我找不到答案。

即使在这种情况下,它也会掉落相同的东西...

'use strict';

const Telegram = require('telegram-node-bot');
var schedule = require('node-schedule');

class AutoMsgController extends Telegram.TelegramBaseController {
    AutoMsgHandler(scope) {
        console.log(scope);
        if (statement) {
            try {
            const button = {'inline_keyboard': [
                [{ text: 'BUTTON1', url: 'URL1' }],
                [{ text: 'BUTTON2', url: 'URL2' }]
            ]}
            const parsedbutton = JSON.stringify(button);
            console.log(parsedbutton);
            scope.api.sendMessage(scope.message._chat._id, '*ACTIVATED!*', { 'parse_mode' : 'Markdown'});
            var j = schedule.scheduleJob('*/5 * * * * *', function(){ //5 Seconds
            scope.api.sendMessage(scope.message._chat._id, 'My message'
            ).then (
            function (e) {
                console.log ('Could not send automsg', e);
                throw e;
              }
            );
    }).catch((err) => {
        console.log('Api call error:', err.message)
      });
        catch (error) {
        console.log('Api call error:', error)
      }
    }
}
    get routes() {
        return {
            'AutoMsgCommand': 'AutoMsgHandler'
        };
    }
}

module.exports = AutoMsgController;
javascript node.js try-catch
1个回答
0
投票

您在尝试后忘记放置catchfinally块。

有关更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch

此外,您应该查看所引发的错误,因为错误表明事务失败。

'use strict';

const Telegram = require('telegram-node-bot');
var schedule = require('node-schedule');

class AutoMsgController extends Telegram.TelegramBaseController {
    AutoMsgHandler(scope) {
        console.log(scope);
        if (statement) {
            try {
            const button = {'inline_keyboard': [
                [{ text: 'BUTTON1', url: 'URL1' }],
                [{ text: 'BUTTON2', url: 'URL2' }]
            ]}
            const parsedbutton = JSON.stringify(button);
            console.log(parsedbutton);
            scope.api.sendMessage(scope.message._chat._id, '*ACTIVATED!*', { 'parse_mode' : 'Markdown'});
            var j = schedule.scheduleJob('*/5 * * * * *', function(){ //5 Seconds
            scope.api.sendMessage(scope.message._chat._id, 'My message'
            ).then (
            function (e) {
                console.log ('Could not send automsg', e);
                throw e;
              }
            );
    }).catch((err) => {
        console.log('Api call error:', err.message)
      });
    }
  } catch (error) {
    // Do error handling stuff
  }
}
    get routes() {
        return {
            'AutoMsgCommand': 'AutoMsgHandler'
        };
    }
}

module.exports = AutoMsgController;
© www.soinside.com 2019 - 2024. All rights reserved.