firebase functions can't deploy. Unexpected token function

Ok so I just found out that firebase functions supports node 8 because I'm getting an error that says "Unexpected token functions" where the first "async" is.

Google says put this in the package json.

"engines": {"node": "8"}

but I put it there and it does nothing. Do any of you know what's wrong?

package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "firebase-admin": "~6.1.0",
    "firebase-functions": "^2.0.3"
  },
  "engines": {"node": "8"},
  "devDependencies": {
    "eslint": "^5.7.0",
    "eslint-plugin-promise": "^4.0.1"
  },
  "private": true
}

index.js

const functions = require('firebase-functions');
const request = require('request');
const admin = require('firebase-admin');
const serviceAccount = require("./service-key.json");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "*"
});

exports.helloWorld = functions.https.onRequest((request, response) => {
 response.send("Hello from Firebase!");
});

exports.getEvents = functions.https.onRequest(() => {
    request.get('*', (error, response, body) => {
        console.log('error:', error);
        console.log('statusCode:', response && response.statusCode);
        console.log('body:', body);

        var data = { "events": body };

        function isCollection(data, path, depth) {
            if (
              typeof data != 'object' ||
              data == null ||
              data.length === 0 ||
              isEmpty(data)
            ) {
              return false;
            }

            for (const key in data) {
              if (typeof data[key] != 'object' || data[key] == null) {
                // If there is at least one non-object item then it data then it cannot be collection.
                return false;
              }
            }

            return true;
          }

          // Checks if object is empty.
          function isEmpty(obj) {
            for(const key in obj) {
              if(obj.hasOwnProperty(key)) {
                return false;
              }
            }
            return true;
          }

          async function upload(data, path) {
            return await admin.firestore()
              // .collection('/lineup2018')
              .doc(path.join('/'))
              .set(data)
              .then(() => console.log(`Document ${path.join('/')} uploaded.`))
              .catch(() => console.error(`Could not write document ${path.join('/')}.`));
          }

          /**
           *
           */
          async function resolve(data, path = []) {
            if (path.length > 0 && path.length % 2 == 0) {
              // Document's length of path is always even, however, one of keys can actually be a collection.

              // Copy an object.
              const documentData = Object.assign({}, data);

              for (const key in data) {
                // Resolve each collection and remove it from document data.
                if (isCollection(data[key], [...path, key])) {
                  // Remove a collection from the document data.
                  delete documentData[key];
                  // Resolve a colleciton.
                  resolve(data[key], [...path, key]);
                }
              }

              // If document is empty then it means it only consisted of collections.
              if (!isEmpty(documentData)) {
                // Upload a document free of collections.
                await upload(documentData, path);
              }
            } else {
              // Collection's length of is always odd.
              for (const key in data) {
                // Resolve each collection.
                await resolve(data[key], [...path, key]);
              }
            }
          }

          resolve(data);
    });
});

EDIT: turns out the function is running on node.js but I still get the error because of async and await. Maybe it's the eslint that is doing something weird.

Upvotes: 0

Views: 2006

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317322

If you want to deploy to node 8, you also need to be using node 8 (or later) to deploy your functions, as the syntax of your functions is checked by the CLI before uploading.

Make sure node --version for your shell shows that node 8 is being used locally.

Also, you may have to delete your function first before changing its runtime at deployment.

Upvotes: 1

Otar Abuselidze
Otar Abuselidze

Reputation: 11

Firebase Functions is still using Node.js 6.x(Google Cloud Functions Node.js 6 Runtime). So async/await keywords are not available at the moment in Firebase Functions.

Here is the beta program for Node.js 8 where you can use async/await.

Upvotes: 0

Related Questions