Telescope144
Telescope144

Reputation: 41

Error when trying to call cloud function to set Firestore

I am trying to add functionality where the app calls a cloud function using HTTPS, and the cloud function stores data in Firestore. I worked through the Google tutorials and other Stackoverflow answers, but did not see an example with similar syntax. Please see below the line that is causing the error "unexpected token admin". Thank you so much!

exports.addData = functions.https.onCall((data, context) => {
    // ...
    const writeResult = await admin.firestore().collection('data-store').add({index: dataToAdd});
  });

Upvotes: 0

Views: 78

Answers (2)

Telescope144
Telescope144

Reputation: 41

I found the issue. "async" should be added to the function call. When I modified the code to the below, the error goes away:

exports.addData = functions.https.onCall(async (data, context) => {
    // ...
    const writeResult = await admin.firestore().collection('data-store').add({index: dataToAdd});
  });

Upvotes: 1

danh
danh

Reputation: 62686

The code presumes you've imported (or required) admin around the top of the file...

// Pre ES2015
var admin = require('firebase-admin');

// modern
import * as admin from 'firebase-admin';

And these presume you've npm installed firebase-admin. More complete instructions here.

Upvotes: 1

Related Questions