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

198
Views
Extraer valores de la solicitud json (que posiblemente podría no estar definido)

Estoy tratando de aprender mecanografiado/js y una de las cosas que estoy haciendo es implementar un rest-api usando aws-lambda (severless tack). Ahora, necesito extraer la carga útil del cuerpo a una construcción o simplemente asignar esos valores a las variables.

Digamos que estoy enviando la siguiente solicitud para registrar un usuario

 POST {{userApiEndpoint}}/signup HTTP/1.1 content-type: application/json {"username":{{username}},"password":{{password}}}

Ahora quiero extraer este nombre de username y password de event.body . He intentado varias cosas y cada vez que recibo errores de string | undefined can't be assigned to string o algo similar.

Cosas que he probado

 export interface User { username: string | undefined; password: string | undefined; }

Opción 1: const newUser = event.body? as User; recibió un error que dice Cannot find name 'as'

Opción 2: const newUser = event.body as User; recibió un error que dice Conversion of type 'string | undefined' to type 'User' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.

Opción 3: const body = JSON.parse(event.body); recibió un error que dice Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

No estoy seguro de qué más probar.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

La opción 3 debería funcionar.

Editar: a menos que haya escrito explícitamente su tipo de event como APIGatewayProxyEventV2 , en cuyo caso el cuerpo es de tipo string | undefined , y el usuario es:

user = JSON.parse(event.body ?? "") as User

 export interface User { username: string | undefined; password: string | undefined; } // event explicitly typed as APIGatewayProxyEventV2 // the body is typed as string | undefined const event = {body: '{"username": "foo", "password": "bar"}'} as Record<string, string | undefined> const user = JSON.parse(event.body ?? "") as User console.log(user.password) // Option 3 // the body is typed as any const event1 = {body: '{"username": "foo", "password": "bar"}'} as Record<string, any> const user1 = JSON.parse(event1.body) as User console.log(user1.password)
about 4 years ago · Juan Pablo Isaza Report
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!