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

136
Vistas
Running a serverless function just returns a Response with a readable stream instead of data

I have the following serverless function

import type { APIGatewayEvent, Context } from 'aws-lambda'
import { logger } from 'src/lib/logger'
const request = require('request')

export const handler = async (event: APIGatewayEvent, context: Context) => {
  const result = await sendToApi(event)

  return {
    statusCode: 200,
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      data: { result },
    }),
  }
}

const sendToApi = function (event) {
  return new Promise((resolve) => {
    logger.info('Invoked twitter function')


    const { twitter } = JSON.parse(event.body)

    const options = {
      method: 'GET',
      url: `https://api.twitter.com/2/users/by/username/${twitter}?user.fields=profile_image_url`,
      headers: {
        Authorization: `Bearer ${process.env.TWITTER_BEARER_TOKEN}`,
      },
      contentType: 'application/json',
    }

    request(options, function (error, response) {
      if (error) throw new Error(error)
      console.log('monkeyboogers', response.body)
      resolve(response.body)
    })
  })
}

and the following front end code that calls the function

const profile = await fetch(
      `${window.location.origin}/.redwood/functions/twitter`,
      {
        method: 'POST',
        body: JSON.stringify({ twitter: twitter }),
      }
    )
    console.log(await profile)
    setTwitter(await profile)

The problem is in the server it console logs the data just fine but in the front end it console logs the following

body: (...)
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"
url: "http://localhost:8910/.redwood/functions/twitter"

and inside the body is just ReadableStream

I know I am missing some key piece, but I am not sure 100% what that is. Thanks for any help ahead of time.l I appreciate it a lot.

about 4 years ago · Juan Pablo Isaza
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