简体   繁体   中英

How to call outer function from inner function in nodejs? I am coding for Google dialogflow fulfillment

  1. Hello friends, Help me out for coding dialogflow fulfillment.
  2. here is the code where i'm invoking GET Api in inner request module and i want to api's output into outer function in conv.ask('Sales is 1 million metric tonnes ' + b)

Code:

var request = require('request');
var code1 = null;
const bodyParser = require('body-parser')
const { dialogflow } = require('actions-on-google');

const assistant = dialogflow({
    clientId: "305xxxx407-rv9kocdxxxxxxxxxciouuq8f9ul2eg.apps.googleusercontent.com"
});

module.exports = (app) => {
        const logger = console;
        assistant.intent('Sales', (conv) => {
            const baseurl = 'https://www.ixxxxxxt.in:3500/getunits?unitcode=4';
            var a = request(baseurl, function(error, res, body) {
                var Unit = JSON.parse(body);
                if (!error && res.statusCode == 200) {
                    var code = JSON.stringify(Unit.description);
                    //res.render(test(Unit));
                    console.log(code); // Print the google web page.
                }
            })
            var b = (a.code);
            console.log(b);
            conv.ask('Sales is 1 million metric tonnes ' + b);
        })

You have a few issues here.

The first is understanding what request() is doing. You probably don't want what request() returns, but instead want access to the body , which you get from the function you define.

That function is actually the second parameter that you've passed to request() . It is referred to as the callback function , since when request() gets the data from the URL, it will call that function. So everything you want to do with body needs to be done inside the callback function.

However, since you're using the Dialogflow library, and this is being done inside an Intent Handler, you need to return a Promise to indicate that you're waiting for a result before it can reply to the user. While you can wrap request() in a Promise, there are better solutions, most notably using the request-promise-native package, which is very similar to the request package, but uses Promises.

This makes things a lot easier. Your code might look something more like this (untested):

var request = require('request-promise-native');
var code1 = null;
const { dialogflow } = require('actions-on-google');

const assistant = dialogflow({
    clientId: "305xxxx407-rv9kocdxxxxxxxxxciouuq8f9ul2eg.apps.googleusercontent.com"
});

module.exports = (app) => {
    const logger = console;
    assistant.intent('Sales', (conv) => {
        const baseurl = 'https://www.ixxxxxxt.in:3500/getunits?unitcode=4';
        return request(baseurl)
            .then( body => {
                // You don't need the body parser anymore
                let code = body.description;
                conv.ask('Sales is 1 million metric tonnes ' + code);
            })
            .catch( err => {
                console.error( err );
                conv.ask('Something went wrong. What should I do now?');
            });
    })

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