Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 有时,当intent使用Axios进行大型逻辑和api调用时,agent.add()不起作用_Node.js_Axios_Dialogflow Es - Fatal编程技术网

Node.js 有时,当intent使用Axios进行大型逻辑和api调用时,agent.add()不起作用

Node.js 有时,当intent使用Axios进行大型逻辑和api调用时,agent.add()不起作用,node.js,axios,dialogflow-es,Node.js,Axios,Dialogflow Es,我在dialogflow代理中没有什么意图,我已经使用dialogflow的内联编辑器为其编写了函数,因此在一些意图中,我使用带有某种逻辑的Axios调用了一些API,出于这种意图,它有时无法在测试控制台上提示消息,但可以在控制台中轻松打印日志 我附加了一些虚拟代码-: function someintent(agent){ return new Promise((resolve, reject) => { // Tried using promises as well but

我在dialogflow代理中没有什么意图,我已经使用dialogflow的内联编辑器为其编写了函数,因此在一些意图中,我使用带有某种逻辑的Axios调用了一些API,出于这种意图,它有时无法在测试控制台上提示消息,但可以在控制台中轻松打印日志

我附加了一些虚拟代码-:

function someintent(agent){

    return new Promise((resolve, reject) => { // Tried using promises as well but didn't work
    const parameter1 = agent.parameters.parameter1;
    const parameter2 = agent.parameters.parameter2;

    // Some of the code is just logic, Problem mainly occurs at line no 39

    if (parameter1 != ''){  



        if (parameter1.toString().length !=9){

            agent.add('Please enter valid 9 digit number');
            agent.context.set({
            'name':'someintent-followup',
            'lifespan': 0
            });

        }

        else{

            if(isNaN(parameter1)){

                agent.add('Please enter integer');
                agent.context.set({
                'name':'previous-followup'
                });
            }


            /**
             * Main area which creates problem is below code.
             * 
             */

            else{

                agent.add('What is the nature of your Business ?'); // This is not working sometimes

                return axios.post('http://some-test-api/',
                    {"parameters1": parameter1}).then((result) => {
                        console.log(result.data);
                        console.log('Store data to api');
                          //agent.add('What is the nature of your Business ?'); // I tried here also which is also not working sometimes



                });

            }
        }
    }

    else{


          agent.add('What is the nature of your Business ?');
          return axios.post('http://some-test-api/',
                    {"parameters2": parameter2}).then((result) => {
                        console.log(result.data);
                        console.log('Store data to api');



                });

    }


    resolve(); // Resolving promise
}
根据我的理解,问题是如果意图是有一个相当大的逻辑,并且也有一些API调用,那么它有超时和回调函数问题(可能),这造成了在接口(测试控制台)中不打印响应的问题


我真的需要帮助。提前感谢。

听起来您有两个潜在问题:

首先,听起来您担心axios的异步调用会花费“太长时间”。如果通话和其他处理过程花费的时间超过几秒钟,这可能是一个问题

但看起来更有可能的是,您如何使用axios和Promises进行通话并使用Promissions引发了一个问题

在显示的代码中,您返回的是新的Promise对象,但在执行代码中,您返回的是axios Promise,因此永远不会调用
resolve()
。仅当异步操作本身已解析时,才应调用
resolve()

但是由于
axios.post()
返回一个承诺,所以您不需要将其包装在自己的承诺中。您只需返回每个axios调用中的Promise/then链

因此,您的代码可能看起来像(未经测试):


非常感谢您的回复,但是您在这里编写的代码,我之前只是这样写的,但它不起作用,这就是我想让承诺成为目标并做出决定的原因。如果你能提出一些建议,我将不胜感激。当我经历一些Github问题和StackOverflow时,我开始了解承诺的概念。我无法得到最佳解决方案。Github上同类问题的链接可能会有所帮助-:
function someintent(agent){

    const parameter1 = agent.parameters.parameter1;
    const parameter2 = agent.parameters.parameter2;

    if (parameter1 != ''){  
        if (parameter1.toString().length !=9){
            agent.add('Please enter valid 9 digit number');
            agent.context.set({
            'name':'someintent-followup',
            'lifespan': 0
            });
        }else{
            if(isNaN(parameter1)){
                agent.add('Please enter integer');
                agent.context.set({
                'name':'previous-followup'
                });
            }else{
                return axios.post('http://some-test-api/',{"parameters1": parameter1})
                    .then((result) => {
                        console.log(result.data);
                        console.log('Store data to api');
                        agent.add('What is the nature of your Business ?');
                    });
            }
        }
    }else{
          return axios.post('http://some-test-api/',{"parameters2": parameter2})
              .then((result) => {
                   console.log(result.data);
                   console.log('Store data to api');
                   agent.add('What is the nature of your Business ?');
               });

    }
}