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 =
{{Test}} = jsonResponse; console.log("Test is " + {{Test}}); return {{Test}}; }) .catch(error => { console.error('Error processing:', error); return 'Error retrieving data'; }); })();
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'; }); })();
4 Replies
added the flow of the bot
You can use
setVariable
like this setVariable(Test, jsonResponse)
https://docs.typebot.io/editor/blocks/logic/script#setvariable-functionThanks @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