简体   繁体   English

如何从nodejs中的内部函数调用外部函数? 我正在为 Google 对话流实现编码

[英]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.你好朋友, 帮我编码dialogflow实现。
  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)这是我在内部请求模块中调用 GET Api 的代码,我想将 api 的输出转换为conv.ask('Sales is 1 million metric tonnes ' + b)外部函数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.首先是了解request()正在做什么。 You probably don't want what request() returns, but instead want access to the body , which you get from the function you define.您可能不想要request()返回的内容,而是想要访问您从定义的函数中获得的body

That function is actually the second parameter that you've passed to request() .该函数实际上是您传递给request()的第二个参数。 It is referred to as the callback function , since when request() gets the data from the URL, it will call that function.它被称为回调函数,因为当request()从 URL 获取数据时,它将调用该函数。 So everything you want to do with body needs to be done inside the callback function.所以你想对body做的一切都需要在回调函数中完成。

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.但是,由于您使用的是 Dialogflow 库,并且这是在 Intent Handler 内完成的,因此您需要返回一个 Promise 以指示您正在等待结果,然后它才能回复用户。 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.虽然您可以request()包装在 Promise 中,但有更好的解决方案,最值得注意的是使用request-promise-native包,它与 request 包非常相似,但使用了 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?');
            });
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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