Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda