What causes the variation in output results between axios and express when using dot notation?

A geo tool application is in the works, built with reactjs. The concept involves users submitting a city name which then triggers a post request. This request searches through a city list.JSON file to find the corresponding city and returns its geolocation information in the following format:

{
      "id": 6167865,
      "name": "Toronto",
      "state": "",
      "country": "CA",
      "coord": {
        "lon": -79.416298,
        "lat": 43.700111
      }
}

The post request on the frontend looks like this:

axios.post('http://localhost:5000/weather/loggedin/citySearch',searchCity)
.then((res)=>{
console.log(res.data.coord)//<--here
})

While on the server-side (backend), the post request is handled as follows:

router.post('/loggedin/citySearch',(req,res)=>{
    let cityname = req.body.cityName
    let countryname = req.body.country
    fs.readFile('../public/weatherdata/citylist.json', (err,data)=>{
        if(err){
            console.log(err)
            res.send('Wrong.')
        }else{

            user_info.cityName = cityname
            let selected_city=(JSON.parse(data)).filter(i=>i.name===cityname && i.country===countryname)
            console.log(selected_city[0].coord)//<--and here
            res.json(selected_city[0])
        }
    })

})

One query that arises is regarding the different outputs obtained when using:

console.log(res.data.coord) //used at axios and it returns the coordinates

as opposed to:

console.log(res.selected_city[0].coord) //used at server.js and it returns undefined

Could it be due to axios and express interpreting dot notation differently?

Answer №1

Ignore the inquiry The selected_city[0] variable was never included in my code

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

What could be causing my middleware to run twice?

A custom middleware was created in express.js/node.js to handle session checking. If a user ID is found in the session, it displays the user's menu; otherwise, it shows the default menu. For every page request, an ID check is performed and the user d ...

What is the best way to display links from one component in another component using react router dom?

In our application, we have a main component, as well as a navbar and sidebar component. Within the sidebar, there are links to movies, games, tv shows, and more. How can we render the movies component within the main component when a link in the sidebar i ...

Guide to Serving PDF Files with Node, Express, and EJS

I've hit a roadblock with what I thought was a simple task. My goal is to create a link that will open a PDF file in a new tab, rather than automatically downloading it. Just to clarify, I already have the PDF file ready and saved. Here's a snip ...

Using react-hook-form to easily update form data

While working on my project with react-hook-form for updating and creating details, I encountered a problem specifically in the update form. The values were not updating properly as expected. The issue seems to be within the file countryupdate.tsx. import ...

Is it possible to execute a node child process with a node script directly from a buffer instead of from a separate file?

At the moment, I am creating a node script in a string/buffer within a parent node script. Subsequently, I write the generated script to a file and then execute that file in a child process. Once completed, I delete the temporary script file. Below is a b ...

Issue encountered while deploying Next.js application on vercel using the replaceAll function

Encountering an error during deployment of a next.js app to Vercel, although local builds are functioning normally. The issue seems to be related to the [replaceAll][1] function The error message received is as follows: Error occurred prerendering page &q ...

The select option fails to automatically assign the selected value from a given response

I am currently working on a project that involves populating two select dropdowns with values from a server. While I am able to successfully retrieve and populate the options of the first select dropdown, I am encountering an issue with setting one of the ...

Tips on preventing Next.js from showing a loading state across the entire page when a specific section of the page sends a request

On my nextjs page, I'm using the loading.tsx approach which displays a loading spinner until the entire page is loaded. This works great, but I have encountered an issue with a popup card that loads upon hovering over a certain element. The problem ar ...

Error message: Unable to render content from layout.js in index.js due to unspecified

I am working with Layout.js and this is the code snippet: import Layout from '../components/Layout'; export default function Home(){ <Layout> test </Layout> } I am trying to display 'test' in m ...

Combining Typescript and React to create a conditional callback prop type depending on whether an optional prop is

In my react component, I have the option to pass an optional prop called isSingle (boolean) and a required prop called onSelect (callback). If the isSingle prop is used, I want the callback to have a different signature. type CustomProps<T> = { ...

The react-router-dom seems to be malfunctioning, so let's simply render the "/"

Struggling to render multiple pages in React, I am a newbie and have been exploring various tutorials and pages. My stack includes React, Webpack, Babel, and ESLint with Airbnb configuration. When I render my React app, it appears like this. View of the ...

NextJS: Error - Unable to locate module 'fs'

Attempting to load Markdown files stored in the /legal directory, I am utilizing this code. Since loading these files requires server-side processing, I have implemented getStaticProps. Based on my research, this is where I should be able to utilize fs. Ho ...

Make Connections between Schemas with MongoDB, Express, and Mongoose

I need to establish relationships between my schemas in the "Movie" entity so that I can access information from other entities like: Category Actor Director Studio Right now, I am testing with categories. This is the code I have written: controllers/m ...

Having trouble getting the express router to function properly in your Node.js TypeScript project?

One of the components in this application is registerClass, where all routes are added. The source code is in the dist directory since this node app is using TypeScript. However, when calling the http://localhost:9001/user endpoint, it seems that it is not ...

What could be the reason for the undefined value of my ID retrieved from the next JS router.query upon page refresh?

When using the id obtained from the next router.query to dynamically render elements, it works fine when accessing the room from next/link. However, upon refreshing the page, an error is thrown. https://i.stack.imgur.com/yEjGS.png Below is the code snipp ...

Is there a way to make all the burger bars change color when hovered over in my ReactJS/NextJS application?

Recently, I encountered an issue with my hamburger menu. When I hover over one of the bars, only that specific one changes color and not all of them. I am struggling to figure out how to make all the bars change color when any one of them is hovered over. ...

Within my React project, I have incorporated a popover component from Material UI. However, I have encountered an issue where the popover does not appear when hovering over the icon

I'm struggling with a certain issue. I have created a popover component that should display when the user hovers over an 'i' icon and disappear when they move away from it. However, it seems like the open and close methods are being continuo ...

In Node.js, the `res.send()` function is called before the actual functionality code is executed

As a newcomer to node js, I am currently working on an app where I query the MySql DB and process the results using node js. One issue I have encountered is that if my initial query returns null data, I then need to perform another query and further proc ...

Error: The function utils.endOfDay does not exist

I recently started using React and integrated the Material-UI date picker in my project by following the guidelines provided in the documentation. For this implementation, I am utilizing moment.js. Below is the code snippet for the datetimepicker: impor ...

What causes NodeJS Express to not trigger the POST handler while the RAM usage increases?

Currently, I am utilizing NodeJs 8.11.3 In my setup, there are two distinct applications running: the first one serves a basic Express' hello world API along with additional libraries: // API.js const express = require('express') const app ...