When using the get function, the HTML text remains unchanged

In my game, I am encountering an issue where the score does not update within my get function every time the game is played. Instead, I keep receiving null property errors. Any assistance on resolving this would be highly appreciated.

I attempted to include the following line inside my get function

const scoreElement = document.getElementById("scoreNums");

scoreElement.innerText = score.correct + "/" + score.total;

This was done in an effort to dynamically change the displayed score after each playthrough.

Below is a snippet of my server-side JavaScript code:

const express = require("express");
const server = express();
const port = 3042;
server.use(express.json());
server.use(express.urlencoded({ extended: true }));


let score = 0;
let correct = 0;

// Middleware for handling cross-domain requests
const allowCrossDomain = function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Methods", "GET,POST");
  res.header("Access-Control-Allow-Headers", "Content-Type");
  next();
};

server.use(allowCrossDomain);

// Route for handling POST requests
server.post("/myPost", function (req, res) {
 score += req.body.score;
 correct += req.body.correct;
 let obj = {correct: correct, score: score};

 return res.status(201).send(obj);
});

// Route for handling GET requests
server.get("/myGet", function (req, res) {
  const scoreData = { correct: correct, score: score };

  return res.status(200).send(scoreData);
});

server.listen(port, function () {
  console.log("Listening on port 3042");
});

Further down the page, additional JavaScript functions are defined for different functionalities within the game such as audio playback, image display, drag and drop functionality, error handling, etc.

Answer №1

It's uncertain if this is the specific problem, but you define the score variable twice in your code at a global level and once inside the server.get function. The variable defined inside the function is overshadowing the global variable.

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

React testing with Mocha experiencing an Invariant violation

Currently, I am in the process of setting up a React project using Electron. Lately, I have been experimenting with configuring Mocha as my test framework. Everything seems to be working fine when I run: "test": "mocha -w --require babel-core/register ...

What is the process of retrieving the header response using Curl?

I am having trouble obtaining the specific response from my curl command. The command successfully inserts data into a list, but I'm unable to capture the desired response. Here is the curl command I am using: curl -d "{"""name""":"""ME""","""id"" ...

Switching ng-show value from separate controller

I'm currently trying to update my ng-show variable whenever a state changes. In my default index.html file, the code looks like this: <div id="searchBar" ng-show="searchBar" ng-controller="mainController"></div> However, since this is no ...

Managing dependencies within my company's shared npm package

Within my company, we have a shared package that has bootstrap as a dependency, and the directory structure is as follows: + - my-shared-package + - node_modules + ... + bootstrap + scss --_mixins.scss --_functi ...

Utilizing the super method within a sails.js controller

Is there a way to call the default parent method of a redefined standard method in a controller created in sails.js? module.exports = { create: function(req, res) { //test some parameters if (condition) { //call regul ...

Modify the CSS selector for a dropdown menu element upon selection

I have customized a select box using CSS, but I am facing an issue where the custom arrow remains upward even after selecting an option. I want the arrow to point downwards once an option is chosen and revert to the default position when clicked outside. ...

Finding the most recent instance of a deeply buried value based on a specific property

I have an example object: const obj = { group: { data: { data: [ { id: null, value: 'someValue', data: 'someData' } ...

Prevent user input in a text box once a radio button is clicked

I have been attempting to create a JavaScript function that will disable a textbox when a radio button is selected. I have four different options for the radio buttons, each with a corresponding textbox. However, even when I select radio button 2, I am s ...

Malfunctioning jQuery Plugin

Currently attempting to implement a jQuery plugin to achieve a pagination effect similar to The plugin I came across is linked at Although the usage seems straightforward, I am encountering difficulties making it function correctly. Displayed below is t ...

Develop a TypeScript class in a distinct file

I currently have ag-grid implemented in an Angular project with a CustomFilter. The problem is that the file containing the code for the CustomFilter function is becoming quite large and difficult to manage. I am now looking to move the CustomFilter to a s ...

Utilize duplicate named CSS grid rows as a reference

Summary; This method currently works, but it requires adding multiple nth-child selectors for each new person and their answer. I am seeking a solution that can dynamically handle any number of rows using an even and odd selector to simplify the process. ...

What is the best way to divide a div by 6?

Is there a way to create six equally sized divisions within a div without using tables or plugins? <div> <div></div> <div></div> <div></div> <div></div> < ...

Guide to accessing a nested and potentially optional object property with a default value and specifying its data type

Just a simple query here... my goal is to extract data.user.roles, but there's a possibility that data may be empty. In such cases, I want an empty array as the output. Additionally, I need to specify the type of user - which in this instance is any. ...

Switching up the toggle button for the bootstrap navbar in a React application

Looking to customize the toggler in React, either by completely changing it or just adjusting the color. Tried solutions from various sources but haven't had any success. 5 Years Ago 1 Year Ago 1 Month Ago The code appears crossed out in dev tools ...

The best way to retrieve information in Express.js is by accessing it from within the

Can I utilize the URL http://localhost:3000/assets/img/1.jpg?50 in place of http://localhost:3000/assets/img/1.jpg/50 app.get('/assets/img/:file/:percentage', (req, res) =>{ loadImagePercent(req, res, "private/img/"+req.param ...

retrieving dynamic data from an HTML form to JavaScript

Currently, I am dynamically adding elements using JavaScript. The count is defined and increases by one. <span style="margin-left:10px;">File Type : <select name="select_type_'+count+'" > <option value="select">Select</optio ...

Choose an image for a better view with the use of HTML and JavaScript/JQuery

Requesting assistance in creating a simple code using plain html5 and JavaScript/jQuery (without plugins) that will enlarge an image upon clicking on it from a list of images. Here is the HTML snippet provided: <!-- Large image holder --> & ...

Tips for aligning the first row of a table with a line of text

I need assistance aligning the first row of a table with a line of text. Specifically, I am trying to achieve ** some text ** |first row of table      |             ...

Steps for inserting an image onto a blank page

Struggling with HTML and javascript - need help with displaying an image on a new page: Creating a new page and want to show an image on it: thepage= window.open('', '', 'height=700,width=800,left=100,top=100,resizable=yes,scroll ...

Altering the background hue of a div with jQuery based on its existing color

As someone new to jQuery, I have been delving into its intricacies and experimenting with it as much as possible. However, I am facing a time crunch and need to complete this project quickly. My question revolves around a particular issue... I have a link ...