Configuring the Port for NodeJS Express App on Heroku

Currently, I am in the process of hosting my website on Heroku and configuring everything to ensure my app is up and running smoothly. However, each time I attempt to submit the form, undefined errors occur.

For more details on the Undefined Errors and Console Errors, you can click on the provided links.

I have set it up to use the port as specified in the documentation:

app.listen(process.env.PORT || 8081, function () {
    console.log('Example app listening on port 8081!');
});

While starting the app locally with heroku local web, I encounter a Typerror: Failed to Fetch along with undefined results. However, upon adding a port=8081 in my .env file, the issue is resolved. Here's an example of a good result

Even after using heroku open, the undefined problem persists.

Do I actually need to set a PORT in the .env file? I have heard that the standard port is 80, but this did not work for me either.

If anyone could provide assistance, I would greatly appreciate it! Thank you!

For reference, here is the link to the public site: . Additionally, here is the link to my Github repository: https://github.com/stefanfeldner/capstone-travel-app

Answer №1

One of the main reasons why these values are undefined is due to their assignment in main.js:

uiData.imageURL = data[1].imageUrl;
...
uiData.iconCode = data[0].iconCode;

The issue lies in the fact that the object retrieved from your /getData endpoint is [{}, {}], resulting in both values being undefined and causing visual inconsistencies.

Why does /getData return empty objects? There could be two potential reasons based on how server.js is structured.

Firstly, an error might be occurring somewhere preventing you from reaching the end of the try-catch block in callApi, resulting in neither weatherData nor pixabayData being updated.

Secondly, it's plausible that the API calls are successful but the desired data is not returned, indicated by neither of the if statements evaluating as true:

if('city_name' in data) {`
...
if('hits' in data) {

In this scenario as well, neither weatherData nor pixabayData would be updated.

Moreover, the handler for /sendFormData only verifies the execution completion of callApi without ensuring useful data retrieval. This results in the code progressing despite the data objects remaining empty.

An unrelated design flaw comes into play here: What happens when multiple users access your website concurrently? The client-side code updates global variables weatherData and pixabayData through /sendFormData and later tries to fetch this data with /getData.

However, between calling /sendFormData and /getData, another user could potentially invoke /sendFormData and alter the global variables before your search results are retrieved. This could lead to receiving their search results instead of yours. Addressing this issue requires handling API results and transmitting them back in a single transaction.

(Regarding the local Heroku Config details, providing assistance without direct access to your system poses challenges, my apologies.)

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

Communication between Angular Controller and Nodejs Server for Data Exchange

Expanding on the solution provided in this thread, my goal is to implement a way to retrieve a response from the node server. Angular Controller $scope.loginUser = function() { $scope.statusMsg = 'Sending data to server...'; $http({ ...

Getting the Tweets of a Twitter-validated user using node.js

I'm struggling with extracting Tweets from a verified user and could use some assistance. I know it's a broad question, but here's the situation: https://github.com/ttezel/twit provides a way to access tweets from any Twitter account. Howev ...

MUI useStyles/createStyles hook dilemma: Inconsistent styling across components, with styles failing to apply to some elements

I have been trying to style my MUI5 react app using the makeStyles and createStyles hooks. The root className is being styled perfectly, but I am facing an issue with styling the logoIcon. Despite multiple attempts to debug the problem, I have not been suc ...

What are the steps to update your profile picture using Angular?

In my Angular 6 application, I am implementing an image upload feature with the following code: Html: <img [src]="url ? url : 'https://www.w3schools.com/howto/img_avatar.png'"> <br/> <input type='file' (change)="onSelec ...

What could be the reason for TypeScript being unable to recognize my function?

In my code, I have a Listener set up within an onInit method: google.maps.event.addListener(this.map, 'click', function(event) { console.log(event.latLng); var lt = event.latLng.lat; var ln = event.latLng.lng; co ...

dynamic jquery checkbox limit

I am working with the following HTML code: <input type="checkbox" id="perlengkapans" data-stok="[1]" onchange="ambil($(this))"> name item 1 <input type="checkbox" id="perlengkapans" data-stok="[4]" onchange="ambil($(this))"> name item 2 &l ...

What could be causing Vuejs to not update elements promptly?

Currently, I am encountering a scenario where I am adding options to a select element using Vue.js when the @change event of that specific element is triggered. An issue arises where the new option is not 'registered' until I exit the function. ...

Make sure to add the local private NPM dependency before running the prepublish script

Within my application package.json file, I am referencing a local private NPM dependency like this: "core-module": "file:///Users/myuser/Documents/projects/core_module" My goal is to have the local private dependencies (such as core-module) automatically ...

Node.js connection is limited to the localhost environment

Recently, I developed a compact Node.js application utilizing connect that not only serves up a webpage but also sends periodic updates while recording user observations to a disk file. Although it works flawlessly on localhost, I encountered difficulty e ...

Not all API results are being displayed by the Nextjs API function

I am facing an issue with rendering all the returns from the API in my Next.js application. The API, which is created in Strapi, is only displaying 1 out of the 3 possible returns. I am a beginner when it comes to using APIs and I suspect that the issue li ...

How can I apply a jquery method to a variable when JavaScript already has a method with the same name?

Is it possible to call the .which function on a character without needing to differentiate between browser types by using the jQuery .which method, which supposedly normalizes for browser discrepancies? I know that the inherent javascript method is also ...

What are the top methods for interacting between Server APIs and Client-Side JavaScript?

Presently, I am utilizing setTimeout() to pause a for loop on a vast list in order to apply some styling to the page. For example, For example: How I utilize setTimeOut: I use setTimeout() to add images, text and css progress bars (Why doesn't Prog ...

Guide on removing query parameters post a redirect using NextJS

Is there a way in NextJS to redirect a URL like /page?foo=bar to /page/bar? I checked out the documentation at https://nextjs.org/docs/api-reference/next.config.js/redirects but couldn't find a solution. This is what I have tried so far: { source: ...

Creating a Slider with a Multitude of Images

I am working on a project to develop a gallery that pulls images from a JSON source. I have successfully implemented infinite scrolling to display the images on the first page. However, I am now facing a challenge when it comes to showing the selected imag ...

How can I resolve the "peerinvalid" error while running NPM install?

My CircleCI suddenly stopped working today due to a peer-invalid issue that has been causing the problem. The error message displayed is as follows: src/index.js -> lib/index.js npm ERR! Linux 3.13.0-91-generic npm ERR! argv "node" "/home/ubuntu/nvm/v0 ...

Display information in a div container from other pages on the website

I am attempting to dynamically load content into a div in Index based on the selection made in a dropdown box, but it doesn't seem to be working correctly. I have created a simple example using four pages to demonstrate the issue: Index.html one.html ...

Tips for resolving the error message "TypeError: Cannot access property 'category' of undefined" while working in a React environment

Hey there, I encountered an issue while trying to destructure in react and can't figure out why the title is appearing as an error message. Below is the code snippet: const {correct_answer, incorrect_answers} = data[0] const arr = [correct_answer, ... ...

Conceal and Reveal every marker on the map

Creating a map using angular to display data on the web page and Google Map is my current project. This is my first major project utilizing Angular, and I have nearly achieved the functionality as planned. I am eager to enhance the site with more user-fri ...

Restarting the timer in JavaScript

How can I reset the countdown timer every time a user types into an input field? I tried using clearTimeout but it doesn't seem to work. Can anyone help me with my existing code instead of providing new code? DEMO: http://jsfiddle.net/qySNq/ Html: ...

Rendering on the server using react-router v4 and express.js

I've been working on implementing server-side rendering with the latest version of react-router v.4. I found a tutorial that I followed at this link: . However, when I refresh the browser, I encounter the following error: Invariant Violation: React.C ...