使用Firebase函数设置无服务器后端。我所看到的设置Functions文件夹的教程都建议使用ESLint特性来捕获可能的bug并强制执行样式。POST lambda路由在本地部署时通知了一个解析错误,但一切仍按需要工作。我要将后端部署到Firebase,我会被抛出一堆错误--因此不允许我继续。我走到它说我有一个错误,删除异步等待,错误消失,但代码中断。我做错了什么?是否仍然有一种方法可以部署我的代码,而不必删除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);
函数文件夹中的.eslintrc.js文件:
module.exports = {
root: true,
env: {
es6: true,
node: true,
},
extends: [
"eslint:recommended",
"google",
],
rules: {
quotes: ["error", "double"],
},
};
函数文件夹中的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
发布于 2021-04-08 08:11:29
在这种情况下,读取堆栈跟踪是个好主意。错误消息指向问题或您可以调试的问题。
如您所见,错误位置在22:47
文件中的index.js
文件中。虽然这并不总是精确的,但是您可以假设错误在这条线上,或者可能在它上面或下面。这方面也有例外。例如,在代码的其他部分,您可能没有封闭的括号。
/Users/Desktop/coding-repos/ecommerce/client/functions/index.js
22:47 error Parsing error: Unexpected token =>
✖ 1 problem (1 error, 0 warnings)
此外,查看日志,因为它们提供了大量的详细信息,以确定正在发生的事情。它们有时很难理解,但是如果您在跟踪中进行反向搜索,您通常会找到所要寻找的内容。
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
许多开发人员会复制并粘贴他们在互联网上找到的代码。当您不确定某件事情是如何工作时,当您需要代码片段时,这是很好的,但是调试时可能会很痛苦,因为开发人员使用旧代码并混合版本。
我对Python比较熟悉,但我猜ESlint在Javascript的一个版本上遇到了问题。看看这个Stackoverflow question,因为我认为它回答了您的问题。解决方案似乎使用了解析器。
"parser": "babel-eslint"
https://stackoverflow.com/questions/66500682
复制相似问题