Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda