【发布时间】:2021-06-04 14:44:46
【问题描述】:
使用 Firebase 函数设置无服务器后端。我看过的设置 Functions 文件夹的教程都推荐使用 ESLint 功能来捕获可能的错误并强制执行样式。 POST lambda 路由在本地部署时通知解析错误,但一切仍按需要工作。我去将后端部署到 Firebase 并抛出了一堆错误 - 因此不让我继续。我转到它说我有错误的行,删除异步等待,错误消失但代码中断。我究竟做错了什么?是否还有一种方法可以部署我的代码,而无需删除 Functions 文件夹并重新进行操作?
functions 文件夹中的index.js 文件:
const functions = require("firebase-functions");
const express = require("express");
const cors = require("cors");
const dotenv = require("dotenv");
dotenv.config();
const stripe = require("stripe")(`${process.env.REACT_APP_STRIPE_SECRET_KEY}`);
// App config
const app = express();
// Middlewares
app.use(cors({origin: true}));
app.use(express.urlencoded({extended: true, useNewUrlParser: true}));
app.use(express.json());
// API routes
app.get("/", (req, res) => res.status(200).send("Hello World"));
// ???? PARSING ERROR PREVENTING DEPLOY ????
app.post("/payments/create", async (req, res) => {
const total = req.query.total;
console.log("Payment Request Received for: , total");
const paymentIntent = await stripe.paymentIntents.create({
amount: total, //sub-units of currency
currency: "USD",
});
res.status(201).send({
clientSecret: paymentIntent.client_secret,
})
});
// Listen command
exports.api = functions.https.onRequest(app);
functions 文件夹中的.eslintrc.js 文件:
module.exports = {
root: true,
env: {
es6: true,
node: true,
},
extends: [
"eslint:recommended",
"google",
],
rules: {
quotes: ["error", "double"],
},
};
functions 文件夹中的 package.json 文件:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "12"
},
"main": "index.js",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"firebase-admin": "^9.2.0",
"firebase-functions": "^3.11.0",
"stripe": "^8.137.0"
},
"devDependencies": {
"eslint": "^7.6.0",
"eslint-config-google": "^0.14.0",
"firebase-functions-test": "^0.2.0"
},
"private": true
}
尝试部署时终端出错:
i deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> functions@ lint /Users/Desktop/coding-repos/ecommerce/client/functions
> eslint .
/Users/Desktop/coding-repos/ecommerce/client/functions/index.js
22:47 error Parsing error: Unexpected token =>
✖ 1 problem (1 error, 0 warnings)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ lint: `eslint .`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/.npm/_logs/2021-03-05T22_26_21_954Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code1
【问题讨论】:
-
刚开始使用云功能时遇到了和你一样的问题。我个人在设置时禁用了 ESLint,但我不建议这样做。这是一个与您的问题非常相似的已回答问题,可以帮助您找到可行的解决方案。 stackoverflow.com/questions/48602833/…
标签: node.js firebase google-cloud-functions eslint serverless