Tips for configuring ejs data within the data attribute and processing it using client-side JavaScript

My aim is to transfer leaderboard information from the server to the client-side JavaScript. This is the code on my server side:

const leaderboard = [[dog,cat],[car,bus],[foo,bar]]

const toJson = JSON.stringify(leaderboard)
        res.render('games/dodge.ejs', {
                leaderboard: toJson
 })    

This is how the ejs file receives it:

<div data-leaderboard="<%= leaderboard%>"></div>

Next, there is the client-side JavaScript file dodge_leaderboards.js

const leaderboardData = document.querySelector("[data-leaderboard]")

console.log(leaderboardData)

Upon running this code, I encounter an error that states Uncaught SyntaxError: Identifier 'leaderboardData' has already been declared (at dodge_leaderboards.js:1:1). Additionally, the console.log returns null.

I am attempting to assign the arrays within the larger array to individual variables, but now I am facing issues with a simple console.log. What do you think might be causing this problem? I am also curious to learn how to parse the array.

Answer №1

Success! I finally fixed it by remembering to add ".dataset.leaderboard" after the variable leaderboardData

const leaderboardData = document.querySelector("[data-leaderboard]")

console.log(leaderboardData.dataset.leaderboard)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

AngularJS service failing to deliver promised result

I am in the process of retrieving data from my database using AngularJS. I have created a service to fetch the data and a controller to display it. Check out my code snippet: angular.module('myApp') .factory('panelService', [&apos ...

Guide on utilizing Multer for uploading a file to separate directories

I have recently started working with nodejs and multer, and I am hoping to upload an image into two separate directories. I attempted to use two different middleware functions but ran into a problem where the first multer function's file destination c ...

Make sure to upload both the original and compressed images when uploading the compressed image

When I upload images, my code compresses them automatically. However, it uploads both the original and compressed versions of the image, as well as an error_log file. I'm not sure why this is happening. How can I modify my code to only upload the comp ...

Creating an Image Link within Nivo Slider

I have been trying to figure out how to make an image a link in NivoSlider. I know that I can use captions to create a link, but I want the actual image to be clickable for better accessibility. I found a similar question on StackOverflow, but it was rela ...

The Cloud Function is experiencing difficulties when attempting to save data to the Map field in Cloud Firestore

I have set up a Webhook to send a JSON payload to my Cloud Function Trigger URL. The purpose of the Cloud Function is to extract data from the JSON and store it in my Cloud Firestore. To test the functionality, I used webhook.site and requestbin.com and ...

Tips for determining the full width of a webpage, not solely the width of the window

While we can easily determine the width of the window using $(window).width(); This only gives us the width of the window itself, not accounting for any overflowing content. When I refer to overflowing, I mean elements that extend beyond the visible view ...

Running Node.js code from npm within Swift can be achieved by following these steps:

I am looking to integrate a Node JS package downloaded from npm into my Cocoa App and run it using Swift. Despite searching online, I have not been able to find any information on whether it is possible to call an npm package from Swift. Can anyone help m ...

What could be causing the submenus in my intricate menu component to lose focus when input is entered?

I'm currently working on developing a menu using the MUI Menu component. The goal is to have a popup input control (such as Autocomplete, TextField, Select, or a custom form) appear when a menu item is clicked, based on the choice made from the menu. ...

An individual in a chat App's UserList experiencing issues with incorrect CSS values. Utilizing Jquery and socketio to troubleshoot the problem

Currently, I am testing a new feature in a chat application that includes displaying a user list for those who have joined the chat. The challenge is to change the color of a specific user's name on the list when they get disconnected or leave the cha ...

What happens when the loading state does not update while using an async function in an onClick event?

I'm currently working on implementing the MUI Loading Button and encountering an issue with changing the loading state of the button upon click. Despite setting the state of downloadLoading to true in the onClick event, it always returns false. The p ...

The connection between Android and the MYSQL database has returned a null value

I have been encountering an issue while trying to fetch specific data from my MySQL database using Android to pass parameter values and then reading this value in a PHP script within the query to retrieve the data. Every time I run the application, I face ...

leveraging external transports in winstonjs

Encountering an issue where the logger is only functioning properly with local strategies when called from an export function inside a controller triggered by HTTP requests, while slack and sentry notifications are not being sent. The setup for the logger ...

Concern raised about the challenge of removing an element from an array and its potential

When attempting to remove an element from an array without altering the state, I typically use the following code snippet: const tempArray = [ ...originalArray ]; tempArray.splice(index, 1); setOriginalArray(tempArray); After some experimentation, I deci ...

store JSON data within an HTML list element

I've been working on a JavaScript script that allows me to generate list items in HTML. Right now, I have functionality to drag and remove the items using another script. However, I am interested in adding some additional information to each list item ...

Filtering: A JSON file that consists of an array of JSON objects

I have a variable prev_data which is a dictionary containing the following values { "values": [ { "color_code": { "bg_color": "#FFD9E1", "border": null, "label&quo ...

The issue arises when React child props fail to update after a change in the parent state

Here's the main issue I'm encountering: I am opening a websocket and need to read a sessionId from the first incoming message in order to use it for subsequent messages. This should only happen once. I have a child component called "processMess ...

Navigating through Sails.Js: A Guide to Implementing Pagination

Looking to implement paginated tables in your application using sails.js, mongodb, and waterline-ORM? Wondering if there is a recommended method for pagination in sails.js? ...

Splitting data in NodeJS sockets

Whenever I encounter the need to divide data, my standard practice involves converting it into a string format. Here's an example of how I handle data in my function: socket.on('data', function (data) { var str = data.toString().spl ...

What is the reason behind the "Error [ERR_HTTP_HEADERS_SENT]" being triggered by using "return next()"?

I built a custom Dialogflow chatbot designed to handle currency conversions. In my fulfillment code, I am utilizing an API to execute the conversion based on user input. However, I encountered an "Error [ERR_HTTP_HEADERS_SENT]" message when using "return n ...

Increase division height when mouse hovers over it

I want the height of the div to be 50px by default and change to 300px onmouseover. I have implemented this as follows: <style type="text/css"> #div1{ height:50px; overflow:hidden; } #div1:hover{ height:300px; } </style> <body> <div i ...