Firebase 函数无法部署。意外的令牌功能



好的,所以我刚刚发现 firebase 函数支持节点 8,因为我收到一个错误,显示第一个"异步"所在的"意外令牌函数"。

谷歌说把它放在包json中。

"引擎": {"节点": "8"}

但我把它放在那里,它什么也没做。你们中有谁知道出了什么问题吗?

包.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
}

索引.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);
});
});

编辑:事实证明该函数正在节点上运行.js但由于异步和等待,我仍然收到错误。也许是埃斯林特在做一些奇怪的事情。

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

确保 shell 的node --version显示节点 8 正在本地使用。

此外,在部署时更改函数运行时之前,可能必须先删除函数。

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

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

最新更新