首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >*ESLint错误*在尝试部署Firebase函数时作出反应

*ESLint错误*在尝试部署Firebase函数时作出反应
EN

Stack Overflow用户
提问于 2021-03-05 23:02:03
回答 1查看 742关注 0票数 0

使用Firebase函数设置无服务器后端。我所看到的设置Functions文件夹的教程都建议使用ESLint特性来捕获可能的bug并强制执行样式。POST lambda路由在本地部署时通知了一个解析错误,但一切仍按需要工作。我要将后端部署到Firebase,我会被抛出一堆错误--因此不允许我继续。我走到它说我有一个错误,删除异步等待,错误消失,但代码中断。我做错了什么?是否仍然有一种方法可以部署我的代码,而不必删除Functions文件夹并再次执行呢?

函数文件夹中的index.js文件:

代码语言:javascript
运行
复制
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文件:

代码语言:javascript
运行
复制
module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  extends: [
    "eslint:recommended",
    "google",
  ],
  rules: {
    quotes: ["error", "double"],
  },
};

函数文件夹中的package.json文件:

代码语言:javascript
运行
复制
{
  "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
}

尝试部署时终端中的错误

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

发布于 2021-04-08 08:11:29

在这种情况下,读取堆栈跟踪是个好主意。错误消息指向问题或您可以调试的问题。

如您所见,错误位置在22:47文件中的index.js文件中。虽然这并不总是精确的,但是您可以假设错误在这条线上,或者可能在它上面或下面。这方面也有例外。例如,在代码的其他部分,您可能没有封闭的括号。

代码语言:javascript
运行
复制
/Users/Desktop/coding-repos/ecommerce/client/functions/index.js
  22:47  error  Parsing error: Unexpected token =>

✖ 1 problem (1 error, 0 warnings)

此外,查看日志,因为它们提供了大量的详细信息,以确定正在发生的事情。它们有时很难理解,但是如果您在跟踪中进行反向搜索,您通常会找到所要寻找的内容。

代码语言:javascript
运行
复制
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,因为我认为它回答了您的问题。解决方案似乎使用了解析器。

代码语言:javascript
运行
复制
"parser": "babel-eslint"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66500682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档