迁移到打字稿后,Firebase云功能不起作用

时间:2020-03-24 05:13:29

标签: reactjs typescript firebase google-cloud-functions

最近,我尝试将Firebase云功能从javascript迁移到打字稿,然后将功能拆分为多个文件。但是,在尝试服务和部署时会不断出错:

投放时出错:

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 Not Running (安装了模拟器,做了firebase init模拟器)
unable to split Firebase functions in multiple files
Firestore local http with real db: The Cloud Firestore emulator is not running so database operations will fail with a 'default credentials' error
https://github.com/firebase/functions-samples/issues/170#issuecomment-586019131
How do I structure Cloud Functions for Firebase to deploy multiple functions from multiple files?
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文件并在export * from './dataDownload;中进行了index.ts时,它也没有起作用。

admin.ts:

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"
  ]
}

编辑https个云功能有效; firestoredatabase云功能没有。

2 个答案:

答案 0 :(得分:5)

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

这为我解决了这个问题。

答案 1 :(得分:0)

我试图从您的代码示例中复制它,但是一切都已正确部署。

根据此doc(您也提到过),lib / index.js为:

在Firebase部署期间,您项目的index.ts将被转换为index.js,这意味着Cloud Functions日志将输出index.js文件中的行号,而不是您编写的代码。为了使您更轻松地在index.ts中找到相应的路径和行号

所以我想您应该检查那里发生了什么,这些文件在您的身边看起来如何。也许您将能够弄清楚。

我也发现可能有帮助的问题:

class imported but still firebase deploy fails with Cannot find module

Firebase deploy can't find serviceAccountKey

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