• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

145
Vistas
How to access variables within a post request?

I'm just starting to learn about HTTP post methods and would like to know if it is possible to get access to variables within the method so that I can export/use it.

Here is my code:

var word = [];
app.post("/search", (req, res) => {;
      console.log(req.body)
      word = req.body["searchedWord"]
    });
console.log(word)

FYI: The req i get looks like { searchedWord: 'soda' }

It currently returns "undefined". If I were to have the console.log(word) within the method, what I want is returned. Any help/tips will be appreciated! Thank you!

almost 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

it looks like you are getting responses in JSON format so you have to do like this

const text = '{"name":"John", "birth":"1986-12-14", "city":"New York"}';
const obj = JSON.parse(text);
obj.birth = new Date(obj.birth);

document.getElementById("demo").innerHTML = obj.name + ", " + obj.birth;


instead of it 
 word = req.body["searchedWord"]
almost 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda