简体   繁体   English

agent.add 在 promise.then 中不起作用

[英]agent.add not working inside promise.then dialogflow

     function issuetype(agent) {
    
          //let i = 0;
       console.log('inside issuetype');
          return admin.database().ref('Support/issuetype').once('value', function(snapshot) {
    
              var data = snapshot.val(); 
              var array = Object.values(data);
            console.log('Issues are');
            console.log(array);
            agent.add(`Select your issue `);  //works fine
            for(const val of array){
              agent.add(new Suggestion(`${val}`)); 
            }
                console.log(data);
          });
       }

   function subtype(agent) {

    let data;
    let value;
    let id;
    console.log('inside subtype');
     let harry = new Promise(function(resolve,reject){
       admin.database().ref('Support/issuetype').once('value', function(snapshot) {
            value = agent.parameters.sub;
            console.log('inside promise');
            data = snapshot.val(); 
            console.log('Key'+Object.keys(data));
            console.log('Value'+value);
            id = Object.keys(data).find(key => data[key] === value);
            console.log('Matched id');
            console.log(id);
           if(id){
             resolve(id);strong text
           }else{
           reject('not resolved');
           }
      });
    });
    
     harry.then(function(res){
        console.log('Type of id:'+typeof(res));
        console.log('id is:'+res);
        agent.add(`Select your sub issue ret`);
       admin.database().ref('Support/issuesubtype/'+res).once('value', function(snap) {
            var snapdata = snap.val(); 
            var values = Object.values(snapdata);
            console.log(typeof(values));  
            console.log('SubIssues are'); // displayed in console
            console.log(values);
            agent.add(`Select your sub issue `); // not displayed
            return agent.add(`Select your sub issue `); // not displayed
           for(const k of values){
            agent.add(new Suggestion(`${k}`)); // not displayed
           }
        });  
    }).catch(function(rej){
        console.log(rej);**strong text**
    }).then(function(rej){
        console.log('Irrespctive');
    });

   }
intentMap.set('issuetype', issuetype);
intentMap.set('subtype', subtype);

Function subtype is called by intentMap, And inside it harry function returns a promise, once promise is resolved I am getting data from firebase and want to display it using agent.add Getting expected output in console.log but agent.add is blank Whereas agent.add is working in issuetype function函数子类型由intentMap 调用,在它内部,harry 函数返回一个promise,一旦promise 得到解决,我将从firebase 获取数据并希望使用agent.add 显示它在console.log 中获取预期输出但agent.add 为空白而agent .add 正在处理 issuetype 函数

Part of the problem is that you're mixing Promises and callbacks, sometimes returning the Promise, and sometimes not.部分问题在于您混合使用 Promise 和回调,有时返回 Promise,有时不返回。 It is easiest if you keep a few guidelines in mind:如果您牢记一些准则,那将是最简单的:

  • Make sure you return the Promise.确保你返回了 Promise。
  • Make sure your call to agent.add() is inside the .then() clause.确保您对agent.add()调用agent.add() .then()子句中。
  • If you're using callback functions, those can be switched to using Promises instead in most cases.如果您正在使用回调函数,则在大多数情况下可以将其切换为使用 Promises。

Keep in mind that you don't need to wrap this into a new Promise, since the Firebase calls you're making return a Promise if you don't give it a callback function.请记住,您不需要将其包装到一个新的 Promise 中,因为如果您不给它一个回调函数,Firebase 调用就会返回一个 Promise。

For example, your line例如,您的线路

admin.database().ref('Support/issuesubtype/'+res).once('value', function(snap) {

should probably better be rewritten as最好改写为

return admin.database().ref('Support/issuesubtype/'+res).once('value')
  .then( snap => {

The important points here are that you're returning the Promise and you're using a Promise instead of a callback to handle the function.这里的重点是您要返回 Promise 并且您正在使用 Promise 而不是回调来处理该函数。

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

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