繁体   English   中英

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

[英]firebase functions can't deploy. Unexpected token function

好的,所以我刚刚发现Firebase函数支持节点8,因为我收到一个错误消息,指出第一个“异步”所在的位置是“意外令牌功能”。

谷歌说把它放在包json中。

“ engines”:{“ node”:“ 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上运行,但由于异步和等待,我仍然收到错误。 也许是在做一些奇怪的事情。

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

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

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

Firebase Functions仍在使用Node.js 6.x( Google Cloud Functions Node.js 6 Runtime )。 因此async/await关键字目前在Firebase Functions中不可用。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM