Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
Javascript: Can't send updated object values to Node/Express

I'm building a web app that should fetch specific stock data for the user's chosen stock from Yahoo Finance and then send this data to a Node/Express server.

PROBLEM: Everything works, except that what is received by Node/Express are the original null-values in the JavaScript object and not the values received from Yahoo Finance.

The initial object variable definitions are first made in index.js:

const stockData = {
    ticker: "",
    name: "",
    dateAdd: 0,
    priceDateAdd: 0,
    priceNow: 0,
    movement: 0
};

The post method is defined:

const options = {
    method: 'POST',
    body: JSON.stringify(stockData),
    headers: {
        "Content-Type": "application/json"
    }

Frontend, the user now chooses a ticker, and a request is sent to Yahoo Finance API to get real values for the stockData object for that ticker. No problems there. E.g. for stockData.priceDateAdd the browser console returns the value 67 for a sample stock.

A function now runs to send the fetched values from Yahoo Finance to Node/Express:

async function sendData() {    
    const response = await fetch('/add', options);
    const serverData = await response.json();
    console.log(serverData);

BUT ... the response from Node/Express shows the original null-values of the object, not the ones fetched from Yahho Finance, i.e., this is the response:

{
  ticker: '',
  name: '',
  dateAdd: 0,
  priceDateAdd: 0,
  priceNow: 0,
  movement: 0
}

I run a second browser console.log after the response from the server, and this continues to show the correct value of stockData.priceDateAdd as 67.

But for some reason the server receives the original null-values. Have spent hours and can't figure it out. Here's hoping for help.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

After more trial and error I have answered my own question:

I moved the const definition inside the function that is being called to send the data, and this worked. So the code is now:

async function sendData() {    
    const options = {
        method: 'POST',
        body: JSON.stringify(stockData), 
        headers: {
            "Content-Type": "application/json"
        }
    };
    const response = await fetch('/add', options);
    const serverData = await response.json();
    console.log(serverData);
}    
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!