简体   繁体   中英

use await inside callback (Microsoft Bot Framework v4 nodejs)

I am trying to send the response back to chatbot emulator from inside callback.

async getUserDetails(step){
    console.log("inside get userdetaiuls modeiule")
    this.userDBObject.password = step.result;

   this.userDBMethod ( async function(response){
        console.log("inside callback return");
        console.log(response);
        await step.context.sendActivity(response); // not able to do this step
        return step.endDialog();
    });
}
async userDBMethod(callback){
request.post('#', 
        {form:{key: 'hi'}}, function (error, response, body) {
        callback("done");
});
}

The error which I'm getting is:

(node:17424) UnhandledPromiseRejectionWarning: TypeError: Cannot perform 'get' on a proxy that has been revoked at D:\\LCI\\Usecases\\statementBalance\\lionsbot-src\\bot.js:384:32 at Request._callback (D:\\LCI\\Usecases\\statementBalance\\lionsbot-src\\bot.js:410:17) at Request.self.callback (D:\\LCI\\Usecases\\statementBalance\\lionsbot-src\\node_modules\\request\\request.js:185:22) at Request.emit (events.js:182:13) at Request.EventEmitter.emit (domain.js:442:20) at Request. (D:\\LCI\\Usecases\\statementBalance\\lionsbot-src\\node_modules\\request\\request.js:1161:10) at Request.emit (events.js:182:13) at Request.EventEmitter.emit (domain.js:442:20) at IncomingMessage. (D:\\LCI\\Usecases\\statementBalance\\lionsbot-src\\node_modules\\request\\request.js:1083:12) at Object.onceWrapper (events.js:273:13) (node:17424) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:17424) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

So how can I use await inside callback to send response back to the user. Thanks !

I would recommend using Axios - a promise based HTTP client for node.js - rather than the request package. Since Axios is promise based, you can use async/await instead of callbacks. The resulting code falls more in line with the flow of the BotFramework. For more details, see the code snippet below and the Axios Documentation .

async getUserDetails(step){
    this.userDBObject.password = step.result;
    try {
        const res = await axios.post('#', {form:{key: 'hi'}});
        await step.context.sendActivity("Done");
    } catch (error) {
        console.log(error);
        await step.context.sendActivity("Sorry, we were not able to complete your request.");
    } 
    return step.endDialog();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM