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}\n\n${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}\n\n${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:
- I created new Intent which will trigger your code once "Test" received.
- I have test the code using different ways, and here the result:
Category_Name = “Cooking”
:
- Dialogflow Agent Test: Worked (https://i.stack.imgur.com/o4Uis.jpg).
- Web Agent Test: Not Worked (https://i.stack.imgur.com/yLWNU.jpg).
- Dialogflow Web Messenger: Worked (https://i.stack.imgur.com/tmSRi.jpg).
- Twilio: Not Worked (https://i.stack.imgur.com/fu4hG.jpg) and error message was (https://i.stack.imgur.com/8rVaz.jpg).
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://i.stack.imgur.com/1be6R.jpg).
- Twilio: Worked (https://i.stack.imgur.com/TKpKF.jpg).
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?