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

161
Vistas
Firebase functions: Value for argument "data" is not a valid Firestore document

I am trying to follow a fireship tutorial with oAuth2 with the code below.

All functions are initialized correctly but there was this error message when I tried to authorize the App through twitter. The error message is below.

⚠ functions: Error: Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field "accessToken"). If you want to ignore undefined values, enable ignoreUndefinedProperties.

The code is below:

const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp()

// Database reference
const dbRef = admin.firestore().doc('tokens/demo')

const TwitterApi = require('twitter-api-v2').default
const twitterClient = new TwitterApi({
  clientId: 'aabbcc',
  clientSecret: 'aabbcc',
})

const callbackURL =
  'http://127.0.0.1:5001/primussoft-74a49/us-central1/callback'

// STEP 1 - Auth URL
exports.auth = functions.https.onRequest(async (request, response) => {
  const { url, codeVerifier, state } = twitterClient.generateOAuth2AuthLink(
    callbackURL,
    { scope: ['tweet.read', 'tweet.write', 'users.read', 'offline.access'] }
  )

  // store verifier
  await dbRef.set({ codeVerifier, state })

  response.redirect(url)
})

// STEP 2 - Verify callback code, store access_token
exports.callback = functions.https.onRequest(async (request, response) => {
  const { state, code } = request.query

  const dbSnapshot = await dbRef.get()
  const { codeVerifier, state: storedState } = dbSnapshot.data()

  if (state !== storedState) {
    return response.status(400).send('Stored tokens do not match!')
  }

  const {
    client: loggedClient,
    accessToken,
    refreshToken,
  } = twitterClient.loginWithOAuth2({
    code,
    codeVerifier,
    redirectUri: callbackURL,
  })

  await dbRef.set({ accessToken, refreshToken })

  const { data } = loggedClient.v2.me() // start using the client if you want

  response.send(data)
})

// STEP 3 - Refresh tokens and post tweets
exports.tweet = functions.https.onRequest(async (request, response) => {
  const { refreshToken } = dbRef.get().data()

  const {
    client: refreshedClient,
    accessToken,
    refreshToken: newRefreshToken,
  } = twitterClient.refreshOAuth2Token(refreshToken)

  await dbRef.set({ accessToken, refreshToken: newRefreshToken })

  //   const { data } = await refreshedClient.v2.tweet(
  //     nextTweet.data.choices[0].text
  //   )

  const { dataone } = { id: 'testid', text: 'a tweet from me' }

  response.send(dataone)
})

exports.tweet = functions.https.onRequest((request, response) => {})

I was actually trying to follow a tutorial by fireship in this link https://youtu.be/V7LEihbOv3Y

Any help would be greatly appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The refreshOAuth2Token method returns a promise as well. Try adding await as shown below:

const {
  client: refreshedClient,
  accessToken,
  refreshToken: newRefreshToken,
} = await twitterClient.refreshOAuth2Token(refreshToken)
about 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda