'firestore database reference doesnt work with await [duplicate]
So I'm trying to use the twitter Api to test functionality and do specific tasks. I decided to use firestore to keep the relevant data. However when I create a database reference and try to use await later on in the code, it gives me an error. This is the code.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const {Firestore} = require('@google-cloud/firestore');
const firestore = new firestore();
const dbRef = firestore.doc('tokens/demo');
const TwitterApi = require('twitter-api-v2').default;
const twitterClient = new TwitterApi({
clientId: 'clientid',
clientSecret: 'clientsecret',
});
const callbackURL = 'http://127.0.0.1:5001/twbt-ad868/us-central1/callback';
// STEP 1 - Auth URL
exports.auth = functions.https.onRequest((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);
});
exports.callback = functions.https.onRequest((request, response) => {
});
exports.tweet = functions.https.onRequest((request, respone) => {});
and this is the error I get
await dbRef.set({ codeVerifier, state });
^^^^^
SyntaxError: await is only valid in async function
I've tried using this code instead to reference the json file in firestore, but I still get the same error
const dbRef = admin.firestore().doc('tokens/demo');
I'm assuming this is because my program isn't properly accessing the database in firestore? When I run this command
gcloud firestore operations list
I get
Listed 0 items.
If this is the case I'm not sure how to fix this and have my code access the database properly
Thank you in advance for any help you can provide me.
Solution 1:[1]
You must create an async function in order to have an await since it requires a promise.
async function () { await dbRef.set({ codeVerifier, state });}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Frank van Puffelen |
