【发布时间】:2020-06-06 09:54:12
【问题描述】:
当我运行我的计划代码来发布消息时,每次发送消息时我都会收到以下错误消息:
未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。 (拒绝编号: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;
'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.sendPhoto(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;
使用此代码取得进展 Api 调用错误:TypeError: schedule.scheduleJob(...).catch is not a function
UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。 (拒绝编号:5)
【问题讨论】:
标签: javascript node.js try-catch