0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I have node v10.13.0, but in the packege.json node used 8 how to solve these
0

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.

2 Comments

wait but look at the package.json, I read the documentation and they said use that engine field and the function will be deployed to node 8. It does nothing.
node 8 is in beta, but publicly accessible. async/await is available there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.