Home > database >  Firebase functions: Value for argument "data" is not a valid Firestore document
Firebase functions: Value for argument "data" is not a valid Firestore document

Time:02-27

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.

CodePudding user response:

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

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