Voiceflow PartnersVPVoiceflow Partners
Powered by
dramatic-maroonD
Voiceflow Partners•9mo ago•
2 replies
dramatic-maroon

Product Array is not parsed

Hi gents

I am new to Voiceflow and I am playing around with a webshop assitant. New when query the shopping cart (API call to an mySQL server returning the products in the JSON format), I am not able to generate a trace message for each article. I always get various errors.

When I hard code the content of the input variable, it works.

Here my code:

export default async function main(args) {
const { products } = args.inputVars;

let productArray;

try {
// Prüfen, ob "products" überhaupt gesetzt ist
if (!products) {
return {
next: { path: 'error' },
trace: [
{
type: 'text',
payload: { message: 'Keine Produktdaten übergeben.' },
},
],
};
}

// Wenn products ein String ist, dann parsen
if (typeof products === 'string') {
const cleanedInput = products.replace(/^json\s*/i, '').trim();
productArray = JSON.parse(cleanedInput);
} else {
productArray = products;
}

// Prüfen, ob Array und nicht leer
if (!Array.isArray(productArray) || productArray.length === 0) {
return {
next: { path: 'no_products' },
trace: [
{
type: 'text',
payload: { message: 'Keine Produkte gefunden.' },
},
],
};
}

// Traces erzeugen
const traces = productArray.map((p) => ({
type: 'text',
payload: {
message:
${p.crtProdAmount ?? '-'} ${p.crtProdEntity ?? ''} ${p.crtProdName ?? 'Unbekannt'} (Art-Nr.: ${p.crtProdNo ?? '---'})
${p.crtProdAmount ?? '-'} ${p.crtProdEntity ?? ''} ${p.crtProdName ?? 'Unbekannt'} (Art-Nr.: ${p.crtProdNo ?? '---'})
,
},
}));

return {
trace: traces,
next: { path: 'success' },
};
} catch (error) {
return {
next: { path: 'error' },
trace: [
{
type: 'text',
payload: { message:
Fehler beim Verarbeiten: ${error.message}
Fehler beim Verarbeiten: ${error.message}
},
},
],
};
}
}
Voiceflow Partners banner
Voiceflow PartnersJoin
Exclusive community for Voiceflow Solutions Providers & Content Creators.
13,076Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements
Recent Announcements
jacklyn

# Let your agents search the web 🔎 Hey everyone! We just released the web search tool for the agent step! Your agents can now automatically search the web for information, letting your agent supplement the LLM's knowledge and the data in its knowledge base with live, up-to-date information. Plus.... * You can restrict searches to specific domains, so your agent only searches sites that you own * This is a tool, so you remain in control of when the agent searches the web * Results are automatically summarized in a way that your agent can automatically understand Under the hood, we're using OpenAI's web search API. Give it a try, and let us know what you think! https://docs.voiceflow.com/changelog/native-web-search-tool

jacklyn · 4mo ago

jacklyn

jacklynbiggin's Thread

jacklyn · 4mo ago

hurt-tomato

connor_maclean's Thread

hurt-tomato · 4mo ago

Similar Threads

Array Variables?
verbal-limeVverbal-lime / ❓┃ask-a-question
2y ago
Where is default customer support agent getting its product information?
RobertFRRobertF / ❓┃ask-a-question
8mo ago
Shopify product database to voiceflow
sad-indigoSsad-indigo / ❓┃ask-a-question
2y ago
Array filter function
moderate-tomatoMmoderate-tomato / ❓┃ask-a-question
2y ago