I am practicing fetching API data and displaying it on a web page. I am using Go to fetch quotes data from an API service. I want an additional slideshow functionality for the web page that should display another fetched quote after 5 seconds using JavaScript.
However, only one quote is displayed on the web page, and no error is logged on the browser's console. The only error my text editor displays is on the HTML file from the snippet below.
<script>
var quotes = {{.QuotesJSON}};
</script>
The following errors are being displayed:
- Property assignment expected.javascript
- Property destructuring pattern expected.javascript
- Declaration or statement expected.javascript
I have tried declaring a field QuotesJSON in the data struct which is of type template.JS to indicate that the string being passed in should be treated as JavaScript code, allowing it to be safely embedded in HTML without being escaped using Go.
data := struct {
QuotesJSON template.JS
}{
QuotesJSON: template.JS(quotesJSON),
}
Where could the issue lie?
{{.QuotesJSON}}is, but it's not javascript. You need to assign a valid JS value to your var.