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

174
Vistas
Javascript global variable won't change after method

If the button is pressed it should first get a value from a SELECT SQL statement and use the result for a INSERT. i got the global variable:

var CostperMovie; 

This is the get function that saves the value in CostperMovie:

const movienumber= req.body.movienumber;    
var sql = "SELECT costs FROM movieWHERE movienumber= ' " + movienumber+" ' ";
connection.query(sql, (err, res)=>{
     if(err)throw err;
        CostperMovie = JSON.parse(JSON.stringify(res[0].costs));
        console.log(CostperMovie);
  });

These are the variables that should be inserted but the variable CostperMovie stays undefined:

const clientID= req.body.clientID;
const begin= req.body.begin;
const end= req.body.end;
const begin1= new Date(req.body.begin).valueOf();
const end1= new Date(req.body.end).valueOf();
const diff = Math.abs(rueckgabedatum1-ausleihdatum1);
const diffDays = Math.ceil(diff / (1000*60*60*24));
const totalCosts= diffDays*CostperMovie;

This is the INSERT with the variables:

const sqlInsert = "INSERT INTO ausleihen (movienumber, clientID, begin, end,totalCosts) VALUES (?,?,?,?,?);";
connection.query(sqlInsert, [movienumber, clientID, begin, end,totalCosts], (err, result)=>{
    console.log(err);
});

So why stays the variable CostperMovie undefined or how can I fix it?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

What is stored in res[0].costs? Do you see the actual data coming in res? Is it valid json? Also, probably, the data you want to be extracted from the json, just doesn't exist. It may be just res.costs or res[0][0].costs etc.

or try something like this:

const result = JSON.parse(JSON.stringify(res));
CostperMovie = result[0].costs;
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