Agent unable to print all results received from Axion library request in Dialogflow

Solution for Agent unable to print all results received from Axion library request in Dialogflow
is Given Below:

I’m trying to print all results received from the following request (this code not working):

function searchForProducts(agent) {
   // category_name="Cooking" for example
   const category_name = agent.parameters.category_name;  
   return new Promise((resolve, reject) => {
       axios.get(`https://sheetdb.io/api/v1/qvlk728a5p23g/search?Categories=*${category_name}*&Status=1`).then(function (res) {
           let Categories = res.data[0];

           if (Categories) {                
                for(var i=0;i<res.data.length;i++){
                    agent.add(`https://alaswadtrading.com/index.php?route=product/product&product_id=${Categories.ProductID}nn${Categories.Name}`);
                }
           } else {
                agent.add(`No items found in the selected category (${category_name})`);
           }

           resolve();
       });
   });      
}

The problem I’m facing is the agent can only print on result through the following code (this record working but return only one URL):

function searchForProducts(agent) {
   const category_name = agent.parameters.category_name;  
   return new Promise((resolve, reject) => {
       axios.get(`https://sheetdb.io/api/v1/qvlk728a5p23g/search?Categories=*${category_name}*&Status=1`).then(function (res) {
           let Categories = res.data[0];

           if (Categories) {
                agent.add(`https://alaswadtrading.com/index.php?route=product/product&product_id=${Categories.ProductID}nn${Categories.Name}`);
           } else {
                agent.add(`No items found in the selected category (${category_name})`);
           }

           resolve();
       });
   });  
    
}

What I’m doing wrong?

============ After Applying Proposed Solution ================

Hello, since yesterday I’m testing but with no luck to get the exact problem. Here what I did:

  1. I created new Intent which will trigger your code once “Test” received.
  2. I have test the code using different ways, and here the result:

Category_Name = “Cooking”:

– Dialogflow Agent Test: Worked (https://imgur.com/sov6Th5).

– Web Agent Test: Not Worked (https://imgur.com/15qxgdR).

– Dialogflow Web Messenger: Worked (https://imgur.com/5ajzd2j).

– Twilio: Not Worked (https://imgur.com/fsrYtDG) and error message was (https://imgur.com/jP6TRbZ).

But, when I changed the Category_Name = “Small%20Appliances”:

– Dialogflow Agent Test: Worked (https://imgur.com/undefined).

– Web Agent Test: Worked (https://imgur.com/undefined).

– Dialogflow Web Messenger: Worked (https://imgur.com/rCn8ksT).

– Twilio: Worked (https://imgur.com/kfXGqTf).

Here a link for web test (I will keep Category_name=’Cooking’) so you can see the result:

https://bot.dialogflow.com/004077c2-d426-472c-89f0-4997e2955d59

What do you think?

Using your code above I was able to loop through agent.add() and got the results. I used test as user input that matches intent “test intent”.

Here is a snippet of the 29 similar outputs of your code provided:

enter image description here


Here is the full code:

index.js

'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const axios = require('axios'); 
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });


  function yourFunctionHandler(agent) {
   const category_name="Cooking";  
   return new Promise((resolve, reject) => {
       axios.get(`https://sheetdb.io/api/v1/qvlk728a5p23g/search?Categories=*${category_name}*&Status=1`).then(function (res) {
           console.log(res.data);
           let Categories = res.data[0];

           if (Categories) {                
                for(var i=0;i<res.data.length;i++){
                    agent.add(`https://alaswadtrading.com/index.php?route=product/product&product_id=${Categories.ProductID}nn${Categories.Name}`);
                }
           } else {
                agent.add(`No items found in the selected category (${category_name})`);
           }

           resolve();
       });
   }); 
  }

  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('test intent', yourFunctionHandler);
  agent.handleRequest(intentMap);
});

package.json

{
  "name": "dialogflowFirebaseFulfillment",
  "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": "10"
  },
  "scripts": {
    "start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
    "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
  },
  "dependencies": {
    "firebase-functions": "^2.0.2",
    "firebase-admin": "^5.13.1",
    "googleapis": "^27.0.0",
    "actions-on-google": "2.2.0",
    "dialogflow-fulfillment": "0.6.1",
    "axios": "0.21.1"
  }
}

Just a suggestion, since your objective is to show all products under a certain “category” you might want to loop through the whole res.data. In this example I just used category_name="Cooking" and printed all products that is under the specified category.

function yourFunctionHandler(agent) {
   const category_name="Cooking";  
   return new Promise((resolve, reject) => {
       axios.get(`https://sheetdb.io/api/v1/qvlk728a5p23g/search?Categories=*${category_name}*&Status=1`).then(function (res) {
           let Categories = res.data;
           if (Categories) {                
                 for (const product of Categories ){
                    agent.add(`https://alaswadtrading.com/index.php?route=product/product&product_id=${product.ProductID}nn${product.Name}`);
                }
           } else {
                agent.add(`No items found in the selected category (${category_name})`);
           }

           resolve();
       });
   }); 
  }

This will result to:
enter image description here