迁移到typescript后,Firebase云功能无法工作



最近,我尝试将我的firebase云函数从javascript迁移到typescript,并将这些函数拆分为多个文件。然而,我在尝试服务和部署时不断出错:

服务时出错:

functions[functionName]:函数被忽略,因为firestore模拟器不存在或未运行。functions[functionName]:函数被忽略,因为firebase模拟器不存在或未运行。

部署时出错

functions[dataDownload(us-central1)]: Deployment error.
Function failed on loading user code. Error message: Code in file lib/index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /srv/node_modules/fs-extra/lib/mkdirs/make-dir.js:86
} catch {
^
SyntaxError: Unexpected token {
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/srv/node_modules/fs-extra/lib/mkdirs/index.js:3:44)

已尝试:
Firestore/Firebase Emulator未运行(安装了模拟器,执行了firebase init模拟器(
无法将firebase函数拆分到多个文件中
Firestore本地http与真实数据库:Cloud Firestore模拟器未运行,因此数据库操作将失败,并显示';默认凭据';错误
https://github.com/firebase/functions-samples/issues/170#issuecomment-586019131
如何为Firebase构建云函数以从多个文件部署多个函数
https://javebratt.com/functions-multiple-files/
https://firebase.google.com/docs/functions/typescript#migrating_an_existing_javascript_project_to_typescript

我的目录结构:

--functions
-- lib
-- node_modules
-- src
-- config
-- admin.ts
-- index.ts
-- dataDownload.ts
-- tsconfig.json
-- tslint.json
-- package.json
-- package-lock.json
--.firebaserc
-firebase.json
--package.json
--package-lock.json

文件:
index.ts:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);
import { dataDownloadHandler } from './dataDownload';
export const dataDownload = functions.firestore.document('user/{uid}/download/{downloadId}').onCreate((snapshot, context) => {
dataDownloadHandler(snapshot, context);
});

dataDownload.ts:

/* 
imports
*/
export const dataDownloadHandler = (snapshot:any, context:any) => {
// code
}

当我将dataDownload函数移到dataDownload.ts文件中并在index.ts中执行export * from './dataDownload;时,它也不起作用。

管理员:

import * as admin from 'firebase-admin';
const auth = admin.auth();
const rtDb = admin.database();
const fsDb = admin.firestore();
const firebaseTimestamp = admin.database.ServerValue.TIMESTAMP;
const firestoreTimestamp = admin.firestore.FieldValue.serverTimestamp();
export { admin, auth, rtDb, fsDb, firebaseTimestamp, firestoreTimestamp };

package.json:

{
"name": "functions",
"engines": {
"node": "8"
},
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"watch": "tsc --watch",
"serve": "npm run build && firebase serve --only functions -P staging",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "npm run build && firebase deploy --only functions",
"logs": "firebase functions:log -P staging"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.5.0",
...other dependencies
},
"devDependencies": {
"eslint": "^5.16.0",
"eslint-plugin-promise": "^4.2.1",
"tslint": "^6.1.0",
"typescript": "^3.8.3"
},
"private": true
}

firebase.json:

{
"database": {
"rules": "database.rules.json"
},
"hosting": [
...],
"storage": {
"rules": "storage.rules"
},
"functions": {
"predeploy": [
"npm --prefix "$RESOURCE_DIR" run lint",
"npm --prefix "$RESOURCE_DIR" run build"
],
"source": "functions"
},
"emulators": {
"functions": {
"port": 5001
},
"firestore": {
"port": 8080
},
"database": {
"port": 9000
},
"hosting": {
"port": 5000
}
}
}

tsconfig.json:

{
"compilerOptions": {
"allowJs": true,
"module": "commonjs",
"resolveJsonModule": true,
"esModuleInterop": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2017"
},
"compileOnSave": true,
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}

EDIThttps云功能正常工作;CCD_ 5和CCD_。

如果您正在使用"fs-extra": "^9.0.0",请尝试降级到版本8.1.0

这为我解决了问题。

我试图从您的代码示例中复制它,但一切都部署得很好。

根据这个文档(你也提到过(lib/index.js是:

在firebase部署期间,您的项目的index.ts被转换为index.js,这意味着云函数日志将输出index.js文件中的行号,而不是您编写的代码。为了让您更容易在index.ts中找到相应的路径和行号

所以我想你应该检查一下那里发生了什么,这些文件在你这边看起来怎么样。也许你会想明白的。

此外,我还发现了一些可能有用的问题:

类已导入,但仍有firebase部署失败,无法找到模块

Firebase部署可以';找不到serviceAccountKey

检查您的引用是否不是本地文件,也许您可以尝试在本地重新创建项目,然后进行部署。我希望这会有所帮助!

相关内容

  • 没有找到相关文章

最新更新