saurav
saurav

Reputation: 5926

Unhandled promise rejection while using Microsoft bot framework

I am using microsoft bot framework and trying to integrate it with external HTTP calls.

However when i invoke the BotWorker.say in the handler BotKitConversation's ask i start getting

 (node:5711) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot perform 'get' on a proxy that has been revoked

Below is my code

In my handler function's callback i get some values returned from external function. Then i try to perform bot.say on the response returned and i recieve the above mentioned error.

 myDialog.ask('What would like to hear?', [
    {
        pattern: '.*',
        handler: async (response, convo, bot) => {
            await YoutubeHelper.getChannel(response, convo,async function(channels){
            console.log("value returned " + channels.length);
            try {
            await bot.say('Printing values'); //error comes here

            if (channels.length == 0) {

                await bot.say('No items found.');
              } else {
                await bot.say('This items\'s ID is %s. Its title is \'%s\', and ' ,
                            channels[0].id,
                            channels[0].snippet.title
                            );
              }
            }catch (err) {
                console.log('error occurred' , err);

            }
            });
        }
    }

],  {key: 'name'});
}

where myDialog is an object of BotkitConversation.

Below is the code for my external utility class

 /**
 * Lists the names and IDs of up to 10 files.
 *
 * 
 */

var {google} = require('googleapis');

var {myDialog} = require("./bot")

const getChannel = function getChannel(searchTerm, convo,callback) {


    var service = google.youtube({
      version : 'v3',
      auth : '<client id>'});


    service.search.list({
      part: 'id,snippet',
      q: searchTerm
    }, function(err, response) {
      if (err) {
        console.log('The API returned an error: ' + err);
        return;
      }
      var channels = response.data.items;
      if (channels.length == 0) {
        console.log('No items found.');
      } else {
        console.log('This items\'s ID is %s. Its title is \'%s\', and ' ,
                    channels[0].id,
                    channels[0].snippet.title
                    );
      }
      console.log(channels.length);
      callback(channels);
    });
    //

  }

  module.exports ={
    getChannel
}

I found this document regarding the error . I am coding according to the guidelines mentioned.

Best Regards,

Saurav

Upvotes: 2

Views: 330

Answers (2)

saurav
saurav

Reputation: 5926

Problem was with my helper class which was not an awaitable function.

I made changes in my helper function to make it return a promise and it started working.

Below is the updated code

 function askMusicPreferences(answer, convo, bot){


myDialog.ask('What would like to hear?', [
    {
        pattern: '.*',
        handler: async(response, convo, bot, message) => {
            try {
            var channels = await YoutubeHelper.getChannel(response);

            if (channels.length == 0) {

               await bot.say('No items found.');
            }
               else {
                await bot.say(`This items\'s ID is ${channels[0].id}. Its title is ${channels[0].snippet.title}`);
              }

            }catch (error){
                console.log( 'error occurred ', err);
            }


       // }
        // catch (err){
        //     console.log('error occurred', err);
        // }

    }
}
],  {key: 'name'});
}

My helper class

    /**
 * Lists the names and IDs of up to 10 files.
 *
 * 
 */

var {google} = require('googleapis');

var {myDialog} = require("./bot");


const getChannel = function getChannel(searchTerm) {
    return new Promise(function(resolve,reject) {

    var service = google.youtube({
      version : 'v3',
      auth : '<client id>'});


    service.search.list({
      part: 'id,snippet',
      q: searchTerm
    }, function(err, response) {
      if (err) {
        console.log('The API returned an error: ' + err);
        reject(err);
        return;
      }
      var channels = response.data.items;
      if (channels.length == 0) {
        console.log('No items found.');
      } else {
        console.log('This items\'s ID is %s. Its title is \'%s\', and ' ,
                    channels[0].id,
                    channels[0].snippet.title
                    );
      }
      console.log(channels.length);
      resolve(channels);
      //callback(channels);
    });
    //
  }); 

  }

  module.exports ={
    getChannel
}

Upvotes: 0

Saeed
Saeed

Reputation: 5488

Try to put first await function in a try-catch block:

myDialog.ask('What would like to hear?', [
  {
    pattern: '.*',
    handler: async (response, convo, bot) => {
      try {
        await YoutubeHelper.getChannel(response, convo, async function (channels) {
          console.log("value returned " + channels.length);
          try {
            await bot.say('Printing values'); //error comes here

            if (channels.length == 0) {
              await bot.say('No items found.');
            }
            else {
              await bot.say('This items\'s ID is %s. Its title is \'%s\', and ',
                channels[0].id,
                channels[0].snippet.title
              );
            }
          }
          catch (err) {
            console.log('error occurred', err);

          }
        });
      }
      catch (error) {
        console.log('----::error::----', error)
      }
    }
  }

], { key: 'name' });

Upvotes: 0

Related Questions