firebase功能无法部署。意外的令牌功能

时间:2018-10-24 17:37:10

标签: javascript node.js google-cloud-functions package.json

好吧,所以我刚刚发现Firebase函数支持节点8,因为我收到一个错误消息,其中第一个“异步”位于“意外令牌功能”。

Google表示将其放在json包中。

“引擎”:{“节点”:“ 8”}

但是我把它放在那里,它什么也没做。你们当中有人知道怎么了吗?

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);
    });
});

编辑:原来该函数正在node.js上运行,但是由于异步和等待,我仍然收到错误。也许是在做一些奇怪的事情。

2 个答案:

答案 0 :(得分:1)

如果要部署到节点8,则还需要使用节点8(或更高版本)来部署功能,因为在上传之前,CLI会检查功能的语法。

确保外壳程序的node --version显示本地正在使用节点8。

此外,您可能必须先删除函数,然后才能在部署时更改其运行时。

答案 1 :(得分:0)

Firebase Functions仍在使用Node.js 6.x(http://curl.haxx.se/libcurl/c/libcurl-errors.html)。 因此,async/await关键字目前在Firebase函数中不可用。

这是Node.js 8的Beta版程序,您可以在其中使用async/await