I’m not a developer may be that’s why I’m struggling with something that should not be difficult but somehow can’t do that for about a week already.
As a result I need to get the ${translator} variable to proceed
To get it I have a php script that looks up the translator and returns it as a text
https://find.dhamma.gift/scripts/translator-lookup.php?fromjs=/sutta/sn/sn1/sn1.1
Output
sv
I need to use the variable in this url for translationResponse.
console.log(
/o/sutta/${slugReady}_translation-ru-${translator}.json`);
It should look like
/o/sutta/sn/sn1/sn1.1_translation-ru-sv.json
But I’m getting this in console log
Url for translationResponse is /o/sutta/sn/sn1/sn1.1_translation-ru-undefined.json
translator outside fetch is sv
But in the code translator is set before the url for translationResponse
I tried many ways that I’ve found on stackoverflow and other sites. But none of them work
Please help. Javascript won this time. I’m defeated.
What I’ve tried
`let translatorData = fetch(‘http://localhost:8080/scripts/translator-lookup.php?fromjs=/sutta/sn/sn1/sn1.1’)
.then(response => response.text());
Promise.all([translatorData]).then(responses => {
const [translator] = responses;
console.log(‘translator outside fetch is’, translator);
});`
Or
`var myGlobalVar = “”;
fetch(‘https://find.dhamma.gift/scripts/translator-lookup.php?fromjs=/sutta/sn/sn1/sn1.1’)
.then(response => response.text())
.then(data => {
window.myGlobalVar = data;
console.log('myGlobalVar is ', myGlobalVar);
// do something with the response data
})
.catch(error => console.error(error));`
Or
`fetch(
`https://find.dhamma.gift/scripts/translator-lookup.php?fromjs=${texttype}/${slugReady}`).then(response => {
return response.text()
})
.then(text => {
console.log(‘text is “‘, text,'”‘)
let translator = text;
console.log(‘translator before fetch “‘, translator, ‘”‘);
})
console.log('translator after fetch "', translator, '"');`
Core fore url for translationResponse works fine if I’ll set variable directly
`let translator = “sv”;
const translationResponse = fetch(
`/o/sutta/${slugReady}_translation-ru-${translator}.json`
).then(response => response.json());`