Используемая ссылка в javascript недоступна внутри блока catch - PullRequest
1 голос
/ 17 октября 2019

Внутри нашего Javascript-кода у нас есть блок try ... catch. Класс работает следующим образом:

const ourCustomClassFile = require ('./ customFile');

Внутри customFile.js мы определили функцию

const sendErrorNotification = (source, reason, scriptposition) => 
{ ...something and write a mail... 
}

наша основная программа получила блок try-catch для всего скрипта следующим образом:

const ourCustomClassFile = require('./customFile');
try{
   const inputFolder = this.config.folder.input;    
   const workFolder = this.config.folder.work;  
   const errorFolder = this.config.folder.error;
}catch(error){
   if (fs.existsSync(workFile)) {
               fs.renameSync(workFile, errorFile);
        }

   sendErrorNotification(
        file,
        `Errortext: ${error}`,
        actPosition
           );   
}

Функция sendErrorNotification прекрасно работает внутри нормального кода нашей основной программы, но внутри блока catchмы получаем исключение:

UnhandledPromiseRejectionWarning: ReferenceError: sendErrorNotification не определено

Так что мне просто нужно знать: почему функция не определена?

1 Ответ

4 голосов
/ 17 октября 2019

внутри вашего customFile.js

const sendErrorNotification = (source, reason, scriptposition) => { 
  ...something and write a mail... 
}
module.exports = {
  sendErrorNotification: sendErrorNotification
}

затем, в вашем файле main.js, назовите его так:

const ourCustomClassFile = require('./customFile');
...
ourCustomClassFile.sendErrorNotification(..)
...
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...