发布在angular应用中部署firebase函数



我目前的问题是,我试图部署一个基本功能到firebase云功能,但我一直得到两个不同的错误。第一个错误是关于两个参数具有隐式any类型。从我在其他帖子上读到的,我所需要做的就是添加类型来修复它,这就是我所做的。这就是我得到我的第二个错误,我只在网上找到了另一个有同样问题的帖子。这个问题的解决方案涉及从他拥有的一个单独的函数中删除类型,这并不真正适用于我。这个错误说"意外的标记;"并且引用参数类型声明中的冒号。我没有启用lint,大多数关于意外令牌的帖子都有作为解决方案,我宁愿不通过关闭严格来禁用类型检查,所以除了这两个解决方案之外,我还没能找到其他任何东西。这个错误似乎是在firebase试图解析函数触发器时发生的,但这对我来说仍然没有多大帮助。我将在下面添加所有可能有用的相关图像和代码。如果你还想看什么,我也可以加进去。谢谢。

编辑2:什么也让我困惑的是,当我不添加任何类型的参数,我能够运行模拟器,一切工作时,我测试他们。只有当我尝试部署时才会出现问题。

当前云功能index.ts

const functions = require("firebase-functions");
const axios = require("axios");
const admin = require('firebase-admin');
const cors = require('cors')({origin: true});
admin.initializeApp();
const db = admin.firestore();
exports.testFunc = functions.https.onRequest((request: any, response: any) => {
cors(request, response, () => {
response.send(request.body)
})
})

项目package.json

{
"name": "local-business-helper",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test"
},
"private": true,
"dependencies": {
"@angular/animations": "~13.0.0",
"@angular/cdk": "^13.0.0",
"@angular/common": "~13.0.0",
"@angular/compiler": "~13.0.0",
"@angular/core": "~13.0.0",
"@angular/fire": "^7.2.0",
"@angular/flex-layout": "^13.0.0-beta.36",
"@angular/forms": "~13.0.0",
"@angular/material": "^13.0.0",
"@angular/platform-browser": "~13.0.0",
"@angular/platform-browser-dynamic": "~13.0.0",
"@angular/router": "~13.0.0",
"axios": "^0.26.1",
"firebase-admin": "^10.1.0",
"firebase-functions": "^3.21.2",
"firebase-tools": "^9.23.3",
"geofire-wrapper": "github:tobias74/geofire-js#upgrade-to-v9-modular",
"moment": "^2.29.3",
"moment-timezone": "^0.5.34",
"ngx-google-places-autocomplete": "^2.0.5",
"ngx-material-timepicker": "^5.5.3",
"rxfire": "^6.0.0",
"rxjs": "~7.4.0",
"tslib": "^2.3.0",
"zone.js": "~0.11.4"
},
"devDependencies": {
"@angular-devkit/build-angular": "~13.0.1",
"@angular/cli": "~13.0.1",
"@angular/compiler-cli": "~13.0.0",
"@types/jasmine": "~3.10.0",
"@types/node": "^12.11.1",
"jasmine-core": "~3.10.0",
"karma": "~6.3.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.0.3",
"karma-jasmine": "~4.0.0",
"karma-jasmine-html-reporter": "~1.7.0",
"typescript": "~4.4.3"
}
}

功能package.json

{
"name": "functions",
"scripts": {
"build": "tsc",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "16"
},
"main": "src/index.ts",
"dependencies": {
"firebase-admin": "^10.0.2",
"firebase-functions": "^3.21.2"
},
"devDependencies": {
"firebase-functions-test": "^0.2.0",
"typescript": "^4.5.4"
},
"private": true
}

类型错误:

src/index.ts:20:47 - error TS7006: Parameter 'request' implicitly has an 'any' type.
20 exports.testFunc = functions.https.onRequest((request, response) => {
src/index.ts:20:56 - error TS7006: Parameter 'response' implicitly has an 'any' type.
20 exports.testFunc = functions.https.onRequest((request, response) => {
Found 2 errors in the same file, starting at: src/index.ts:20
Error: functions predeploy error: Command terminated with non-zero exit code2

意想不到的令牌:

Error: Error occurred while parsing your function triggers.
/Users/......../functions/src/index.ts:20 
exports.testFunc = functions.https.onRequest((request: Request, response: any) => {
SyntaxError: Unexpected token':'
at Object.compileFunction(node: vm: 352:18)
at wrapSafe(node: internal/modules/cjs/loader:1031:15)
at Module.compile(node: internal/modules/cjs/loader:1065:27)
at Object.Module.extensions.js(node: internal/modules/js/loader:1153:10) 
at Module.load(node: internal/modules/cjs/loader:981:32) 
at Function.Module._load(node: internal/modules/cjs/loader:822:12) 
at Module.require(node: internal/modules/cjs/loader:1005:19) 
at require(node: internal/modules/cjs/helpers:102:18) 
at loadModule(/usr/local/lib/node_modules/firebase-tools/lib/deploy/functions/runtimes/node/triggerParser.js:10:16) 
at /usr/local/lib/node_modules/firebase-tools/lib/deploy/functions/runtimes/node/triggerParser.js:34:21

编辑:注意到有一个更新后,我更新了函数和错误格式略有改变,但它仍然是相同的。

functions: Finished running predeploy script. 
functions: ensuring required API cloudfunctions.googleapis.com is enabled...
functions: ensuring required API cloudbuild.googleapis.com is enabled... 
functions: required API cloudbuild.googleapis.com is enabled
functions: required API cloudfunctions.googleapis.com is enabled
functions: preparing codebase default for deployment
Error: Failed to load function definition from source: Failed to generate manifest from function source: SyntaxError: Unexpected token':'

虽然这听起来很有争议,但云函数并不理解TypeScript。事实上,不仅仅是Cloud Functions,所有的JavaScript引擎都不理解TypeScript。在编写代码时,总是需要编译器将TypeScript代码构建为JavaScript。然后运行的是构建好的JS代码,而不是TypeScript代码。

是的,TypeScript的Cloud Functions是可用的,并且npm run build -- --watch会在你编码时更新构建的代码。Cloud Functions实际运行的是构建好的JS代码,而不是TypeScript代码。

设置的问题是云功能的入口点是.ts文件而不是.js。在你的包裹里。对于函数,你有

"main": "src/index.ts"

你必须把它改成:

"main": "lib/index.js"

这样,Cloud Functions的入口点将是一个JavaScript文件,也将是由TypeScript代码构建的JavaScript。之所以使用lib/index.js,是因为在默认情况下,tsc命令将在那里构建src/index.ts文件。

您可以检查.gitignore,并注意到lib文件夹被包含在内(因为它的内容总是在每次构建中更改,不应该进入版本控制)。还要注意,在lib文件夹中也包含一个index.js.map文件。事实上,对于src文件夹中的每个TypeScript文件,在lib文件夹中会创建一个相应的.js.js.map文件。

相关内容

  • 没有找到相关文章

最新更新