adverse-sapphire•2mo ago
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 ?? '---'})
,
},
}));
return {
trace: traces,
next: { path: 'success' },
};
} catch (error) {
return {
next: { path: 'error' },
trace: [
{
type: 'text',
payload: { message: Fehler beim Verarbeiten: ${error.message}
},
},
],
};
}
}2 Replies
ratty-blush•2mo ago
is this solved i have same question.
data returned from api request never gets parsed and is never sent to carousal
adverse-sapphireOP•2mo ago
@atta866 unfortunately, no. No reply yet.