Express.js Failing to Send Response Following POST Request

This is the function I have on the back end using express:

// Function to register a new user
exports.register_user = function(req, res) {
    var portalID = req.body.portalID;
    var companyName = req.body.companyName;
    var password = req.body.password;
    var password2 = req.body.password2;

    var salt = bcrypt.genSaltSync(12);
    var hash = bcrypt.hashSync(password, salt);
    password = hash;

    var params = {
        TableName: "HouseAccounts",
        Item: {
            "portalID": portalID,
            "companyName": companyName,
            "points": 0,
            "password": password,
        }
    }
    res.sendStatus(200);
}

And this is the fetch function on the front end:

function register() {
  fetch("MyURL/register", {
    method: "POST",
    headers: {
        "Content-Type": "application/json"
    },
    body: JSON.stringify({
      "portalID": document.getElementById("portal-id").value,
      "companyName": document.getElementById("company-name").value,
      "password": document.getElementById("password").value,
      "password2": document.getElementById("password2").value
    })
  }).then(function(response){console.log(response)});
}

While I can receive the JSON data sent through the POST request on the express side and process it, unfortunately, on the front end, I am not getting any response from the back end. The connection times out with a status of (failed) and an error message

Uncaught (in promise) TypeError: Failed to fetch
in console.

Answer №1

It turns out that the issue stemmed from my use of AWS Cloud9, where my Public IP changed whenever I restarted the instance. This caused the request to not reach the server side at all.

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

Is there a problem with the string comparison in my JavaScript code?

I am dealing with various XML files specific to different operating systems. Here is an excerpt from the SunOS XML: <osname>SunOS </osname> This data is extracted using jQuery: var osname = $(this).find('osname').text(); However ...

What is the method to retrieve a checkbox value within a datatable when employing pagination?

I recently came across an issue with my datatable where I had implemented a checkbox in the first column of each row. However, when I attempted to check the checkbox using an AJAX call, it only worked for the first page and not for any subsequent pages. H ...

Vue.js - Resetting child components upon array re-indexing

I am working with an array of objects const array = [ { id: uniqueId, childs: [ { id: uniqueId } ] }, { id: uniqueId, childs: [ { id: uniqueId } ] }, ] and I have a looping structure ...

Only the initial element within the specified class is targeted by JQuery

Currently, I am utilizing Kendo UI to refresh multiple charts by class without having to access each one individually. Here is the code snippet I am using: $(".k-chart").data("kendoChart").refresh(); The issue I am encountering is that only the first cha ...

Effortlessly switch between multiple divs with jQuery

I am working on a functionality where multiple divs should be displayed or hidden based on the button clicked. Initially, all buttons and divs are visible. Upon clicking a button, only the corresponding div should be visible. Subsequent clicks on other but ...

Is your webpage slow to respond after a page-refresh? (Delayed HTML rendering causing lag)

Whenever I adjust the screen size to that of a phone or any device smaller than 768px, the search bar doesn't display in its proper position until the page is refreshed. It should be correctly placed right from the start. Furthermore, when I resize th ...

What is the best way to implement a JavaScript pattern matching for both "aaaa" and "aaa aaa"?

Can anyone help me create a pattern that can accept both strings with spaces and without spaces in the same text box? I would appreciate any guidance on how to achieve this. Thanks! ...

Generate a request to load JSON data

On my webpage, I have several external JSON files that need to be loaded. I'm looking for a way to specify the order in which they should be loaded. Using JavaScript for this task, here is an example: const func1 = () => { $.getJSON(json1, re ...

POST request results in empty JSON response from Fetch

I have been utilizing the Fetch API to send a POST request to my Express server. However, I am encountering an issue where I consistently receive an empty JSON response ({}) instead of the expected data. var postData = function (url, data) { return fe ...

Double injection of Redux-saga

I've encountered a strange issue with my redux-saga - it's being called twice instead of just once. Here is the action that triggers the saga: export function createRequest (data) { return { type: CREATE_REQUEST, payload: {data} }; ...

Unable to click on link with JavaScript using Selenium webdriver

<a id="compareCompanies" b:onclick="needsController.showQuotes = true;" href="#">Compare companies</a> Below is the Selenium Webdriver JavaScript code using Mocha: driver.wait(function () { driver.findElement(webdriver.By.id("compareCompa ...

What is the best way to ensure that the div from the header file remains fixed directly above the fixed div from another file?

This is the header section that I want to keep fixed within the "header" div. <div id="header" style="display:block;"> <table style="width:100%"> <tr> <td class="col-sm-6" style="background-color:lavender;"><a href ...

Is the Wrapper created by the combination of React, JavaScript Arrow functions, and Classes

I stumbled upon a website at: After implementing the solution successfully, I found myself puzzled by the javascript code. There was a snippet in the AnimatedWrapper.js class that caught my attention: const AnimatedWrapper = WrappedComponent => class ...

Verification of radio status against boolean value stored in the database

Here is what I have: A boolean value is stored in the database, so when "yes" or "no" is checked, the radio button should also be checked and reflect the boolean value in the database. Can someone guide me on how to achieve this? ...

Triggering an automatic pop-up window using JavaScript based on a PHP condition

I have developed a hidden pop-up box that becomes visible when targeted, with its opacity increasing to one. I am aiming for the pop-up to appear if the user is identified as a "firstTimeUser," a status saved in a PHP $_SESSION variable. However, I am stru ...

Having trouble building a query with Sequelize?

Struggling to replicate a specific query in Sequelize. I've spent hours attempting to crack this puzzle. The issue arises from my usage of camelCase instead of snake_case, preventing me from reusing the original query. Converting all snake_cased var ...

Why does the Vue router sometimes refresh the page instead of properly navigating to it? Can you spot the difference between these two code examples?

I am encountering an issue while trying to click on a "checkout" button that is supposed to send cart data to a backend and redirect me to a URL generated by the backend. Despite receiving a valid URL, my code is not functioning correctly. When I attempt ...

Express Session Issue Preventing Ajax Call from Functioning

After testing the /temp route directly in the browser and then through an ajax call to the /test route, I noticed a difference in the session information. When accessed directly, the session retains the data added via the /temp route, but when called throu ...

Can the browser doc mode be switched frequently?

My web application is built using AngularJS, but we also have a legacy web app that functions only in quirks mode and is included via an iframe on one of our pages. The challenge is to make this legacy app work within our main browser-based application, wh ...

Display the div if the input field is left blank

Is there a way to display the div element with id="showDiv" only if the input field with id="textfield" is empty? <form action=""> <fieldset> <input type="text" id="textfield" value=""> </fieldset> </form> <div id="sh ...