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

94
Views
¿Qué puedo hacer para que object.property no devuelva indefinido?

Estoy tratando de crear una función que primero obtenga los tokens de acceso JWT de mi sesión actual y luego los aplique al encabezado de Autorización de mi solicitud real. Esto funciona bien, pero el resultado final es que quiero devolver el objeto de respuesta completo.

Intenté asignar parte de la salida a un objeto de output , pero cuando intento console.log algunas de las propiedades, simplemente devuelve indefinido. Si hago console.log dentro del navegador, entonces puedo ver el resultado pero nunca puedo imprimir las propiedades reales.

 export default function fetcher(url, method, body = null) { const output = {} fetch("/api/auth/session") .then((response) => { return response.json() }) .then((data) => { const token = data.user.accessToken if (method.toLowerCase() == "post") { const res = fetch(url, { method: "post", headers: { Authorization: "Bearer " + token, Accept: "application/json, text/plain, */*", "Content-Type": "application/json", }, body: body, }).then((response) => { Object.assign(output, { status: response.status, }) }) } else { const res = fetch(url, { method: "get", headers: { Authorization: "Bearer " + token, Accept: "application/json, text/plain, */*", "Content-Type": "application/json", }, }) .then((response) => response.json()) .then((data) => { Object.assign(output, data) }) } }) .catch((err) => { // Do something for an error here }) console.log(output) console.log(output.status) return output }

Consola.logs

El objetivo final es llamar a la función desde algún otro archivo como este

 const test = fetcher("http://api.localhost/v1/projects", "post", JSON.stringify({ name: event.target.name.value })) console.log(test.status)
about 4 years ago · Juan Pablo Isaza
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!