Search code examples
javascriptnode.jspromisees6-promisedialogflow-es-fulfillment

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


Solution

  • Part of the problem is that you're mixing Promises and callbacks, sometimes returning the Promise, and sometimes not. It is easiest if you keep a few guidelines in mind:

    • Make sure you return the Promise.
    • Make sure your call to agent.add() is inside the .then() clause.
    • If you're using callback functions, those can be switched to using Promises instead in most cases.

    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.

    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.