Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda