Share feedback, ideas and get community help

Updated 3 months ago

Override Value of a {{Variable}}

Hi Guys:
how to override the value of a {{Variable}}?
This is my (stupid) script.
The {{Test}} variable is not updated, but when i output it in console it is :-/.
What i'm doing wrong?
Thanks!
(function() {
const apiDomain = {{LinkImmo}}; // DO NOT TOUCH
const url = https://corsproxy.io/?${encodeURIComponent(apiDomain)};

fetch(url)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok: ' + response.statusText);
}
return response.text();
})
.then(htmlContent => {
const pattern = /<script id="NEXT_DATA" type="application/json">\s({.?})\s*</script>/s;
const matches = htmlContent.match(pattern);
if (!matches) {
console.error('JSON content not found in HTML');
return 'Error retrieving data';
}
const jsonContent = matches[1];

const data = JSON.parse(jsonContent).props.pageProps.detailData.realEstate.properties[0];
const keysToExclude = ['adLinks', 'cadastrals', 'caption', 'defaultDescription', 'features', 'ga4features', 'id', 'income', 'land', 'multimedia', 'reference', 'rent', 'roomForRent', 'typologyValue'];
keysToExclude.forEach(key => delete data[key]);

const jsonResponse = Object.entries(data).map(([key, value]) => {
if (typeof value === 'object' && value !== null) {
return ${key}\t${JSON.stringify(value)};
}
return ${key}\t${value};
}).join('\n');



{{Test}} = jsonResponse;
console.log("Test is " + {{Test}});
return {{Test}};
})
.catch(error => {
console.error('Error processing:', error);
return 'Error retrieving data';
});
})();
B
B
8 comments
added the flow of the bot
Override Value of a {{Variable}}
You can use setVariable like this setVariable(Test, jsonResponse)
Thanks @Baptiste I need the script executed server side, right? The goal here was executing it client side and then assign to a variable.
One solution that i found is to include the script in the head (via settings) and assign the value of a new var like this {{Hello}} = new Greeting({{Nome}}).greet();
WHERE the script is done this way:
class Greeting {
constructor(name) {
this.name = name;
}

greet() {
return Hello, ${this.name}!;
}
}
Since your code is doing an API request, I suggest that you use the HTTP Requests block instead
Add a reply
Sign up and join the conversation on Discord
/s; const matches = htmlContent.match(pattern); if (!matches) { console.error('JSON content not found in HTML'); return 'Error retrieving data'; } const jsonContent = matches[1]; const data = JSON.parse(jsonContent).props.pageProps.detailData.realEstate.properties[0]; const keysToExclude = ['adLinks', 'cadastrals', 'caption', 'defaultDescription', 'features', 'ga4features', 'id', 'income', 'land', 'multimedia', 'reference', 'rent', 'roomForRent', 'typologyValue']; keysToExclude.forEach(key => delete data[key]); const jsonResponse = Object.entries(data).map(([key, value]) => { if (typeof value === 'object' && value !== null) { return ${key}\\t${JSON.stringify(value)}; } return ${key}\\t${value}; }).join('\\n'); {{Test}} = jsonResponse; console.log(\"Test is \" + {{Test}}); return {{Test}}; }) .catch(error => { console.error('Error processing:', error); return 'Error retrieving data'; });})();","url":"https://community.typebot.io/override-value-of-a-variable-v2B5vzbtk7KR","identifier":"v2B5vzbtk7KR","publisher":{"@type":"Organization","name":"Typebot","logo":{"@type":"ImageObject","url":"https://assets.usehall.com/org_01J5WY498Z4HFNZA1D6GQGBV59/079223ba-f2d1-4825-8243-501026f6f74c.png"}},"comment":[{"@type":"Comment","text":"added the flow of the bot","dateCreated":"2024-04-23T16:14:18.214Z","dateModified":"2024-04-23T16:14:18.214Z","author":{"@type":"Person","url":"https://community.typebot.io/members/fa4ddcc5-dd4c-464e-b3aa-795452a9b011","name":"Bobbit6k","identifier":"fa4ddcc5-dd4c-464e-b3aa-795452a9b011","image":"https://cdn.discordapp.com/avatars/376784565395783681/cceda11652f1d7548095ba986737a34f.webp?size=256"},"commentCount":0,"comment":[],"position":1,"upvoteCount":0},{"@type":"Comment","text":"Override Value of a {{Variable}}","dateCreated":"2024-04-23T18:14:11.588Z","dateModified":"2024-04-23T18:14:11.588Z","author":{"@type":"Person","url":"https://community.typebot.io/members/fa4ddcc5-dd4c-464e-b3aa-795452a9b011","name":"Bobbit6k","identifier":"fa4ddcc5-dd4c-464e-b3aa-795452a9b011","image":"https://cdn.discordapp.com/avatars/376784565395783681/cceda11652f1d7548095ba986737a34f.webp?size=256"},"commentCount":0,"comment":[],"position":2,"upvoteCount":0},{"@type":"Comment","text":"You can use setVariable like this setVariable(Test, jsonResponse)","dateCreated":"2024-04-24T07:20:50.015Z","dateModified":"2024-04-24T07:20:50.015Z","author":{"@type":"Person","url":"https://community.typebot.io/members/589d2b28-a111-4f02-a228-ad757d0e0112","name":"Baptiste","identifier":"589d2b28-a111-4f02-a228-ad757d0e0112","image":"https://cdn.discordapp.com/avatars/621241109913403395/6fd7e5e4b8bca7e84d2c0bbdf9906a08.webp?size=256"},"commentCount":0,"comment":[],"position":3,"upvoteCount":0},{"@type":"Comment","text":"https://docs.typebot.io/editor/blocks/logic/script#setvariable-function","dateCreated":"2024-04-24T07:20:50.638Z","dateModified":"2024-04-24T07:20:50.638Z","author":{"@type":"Person","url":"https://community.typebot.io/members/589d2b28-a111-4f02-a228-ad757d0e0112","name":"Baptiste","identifier":"589d2b28-a111-4f02-a228-ad757d0e0112","image":"https://cdn.discordapp.com/avatars/621241109913403395/6fd7e5e4b8bca7e84d2c0bbdf9906a08.webp?size=256"},"commentCount":0,"comment":[],"position":4,"upvoteCount":0},{"@type":"Comment","text":"Thanks @Baptiste I need the script executed server side, right? The goal here was executing it client side and then assign to a variable.","dateCreated":"2024-04-24T07:28:20.006Z","dateModified":"2024-04-24T07:28:20.006Z","author":{"@type":"Person","url":"https://community.typebot.io/members/fa4ddcc5-dd4c-464e-b3aa-795452a9b011","name":"Bobbit6k","identifier":"fa4ddcc5-dd4c-464e-b3aa-795452a9b011","image":"https://cdn.discordapp.com/avatars/376784565395783681/cceda11652f1d7548095ba986737a34f.webp?size=256"},"commentCount":0,"comment":[],"position":5,"upvoteCount":0},{"@type":"Comment","text":"One solution that i found is to include the script in the head (via settings) and assign the value of a new var like this {{Hello}} = new Greeting({{Nome}}).greet();","dateCreated":"2024-04-24T08:18:42.188Z","dateModified":"2024-04-24T08:18:42.188Z","author":{"@type":"Person","url":"https://community.typebot.io/members/fa4ddcc5-dd4c-464e-b3aa-795452a9b011","name":"Bobbit6k","identifier":"fa4ddcc5-dd4c-464e-b3aa-795452a9b011","image":"https://cdn.discordapp.com/avatars/376784565395783681/cceda11652f1d7548095ba986737a34f.webp?size=256"},"commentCount":0,"comment":[],"position":6,"upvoteCount":0},{"@type":"Comment","text":"WHERE the script is done this way:class Greeting { constructor(name) { this.name = name; } greet() { return Hello, ${this.name}!; }}","dateCreated":"2024-04-24T08:19:29.189Z","dateModified":"2024-04-24T08:19:29.189Z","author":{"@type":"Person","url":"https://community.typebot.io/members/fa4ddcc5-dd4c-464e-b3aa-795452a9b011","name":"Bobbit6k","identifier":"fa4ddcc5-dd4c-464e-b3aa-795452a9b011","image":"https://cdn.discordapp.com/avatars/376784565395783681/cceda11652f1d7548095ba986737a34f.webp?size=256"},"commentCount":0,"comment":[],"position":7,"upvoteCount":0},{"@type":"Comment","text":"Since your code is doing an API request, I suggest that you use the HTTP Requests block instead","dateCreated":"2024-04-24T08:48:18.468Z","dateModified":"2024-04-24T08:48:18.468Z","author":{"@type":"Person","url":"https://community.typebot.io/members/589d2b28-a111-4f02-a228-ad757d0e0112","name":"Baptiste","identifier":"589d2b28-a111-4f02-a228-ad757d0e0112","image":"https://cdn.discordapp.com/avatars/621241109913403395/6fd7e5e4b8bca7e84d2c0bbdf9906a08.webp?size=256"},"commentCount":0,"comment":[],"position":8,"upvoteCount":0}],"author":{"@type":"Person","url":"https://community.typebot.io/members/fa4ddcc5-dd4c-464e-b3aa-795452a9b011","name":"Bobbit6k","identifier":"fa4ddcc5-dd4c-464e-b3aa-795452a9b011","image":"https://cdn.discordapp.com/avatars/376784565395783681/cceda11652f1d7548095ba986737a34f.webp?size=256"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0}}]