简体   繁体   English

在 Firestore 云 function 中使用 Angular 库 (APF)

[英]Using an Angular library (APF) inside a Firestore cloud function

I created a angular library using angular 14 inside a workspace, added a class with a method, now I'm trying to use it inside the functions folder using typescript, the workspace structure is as follows: I created a angular library using angular 14 inside a workspace, added a class with a method, now I'm trying to use it inside the functions folder using typescript, the workspace structure is as follows:

root
   dist
      my-lib
         ...
   functions
      ...
   projects
     my-lib
     my-app

After building the library, I tried using it inside the helloWorld cloud function like this:构建库后,我尝试在 helloWorld 云 function 中使用它,如下所示:

In functions/tsconfig.json under compiler options, added "paths": {"my-lib": ["../dist/my-lib"]} .在编译器选项下的 functions/tsconfig.json 中,添加了"paths": {"my-lib": ["../dist/my-lib"]}

And functions/package.json, added a dependency: "my-lib": "file:../dist/my-lib" ,和functions/package.json,添加了一个依赖: "my-lib": "file:../dist/my-lib"

and then in functions/src/index.ts : import {MyClass} from "my-lib";然后在functions/src/index.tsimport {MyClass} from "my-lib"; . .

Problem: when starting the emulator 'firebase emulators:start --only functions' I get this error:问题:启动模拟器“firebase emulators:start --only functions”时出现此错误:

Error: Failed to load function definition from source: Failed to generate manifest from function source: Error [ERR_REQUIRE_ESM]: require() of ES Module C:\angularproject\dist\my-lib\fesm2015\my-lib.mjs not supported.
Instead change the require of C:\angularproject\dist\my-lib\fesm2015\my-lib.mjs to a dynamic import() which is available in all CommonJS modules.

What's the correct way to include a library from an Angular project in a firebase cloud function?在 firebase 云 function 中包含来自 Angular 项目的库的正确方法是什么?

I managed to get this to work by changing the following:我设法通过更改以下内容来使其工作:

functions/tsconfig.json

"module": "es2020",
"target": "es2020",
"moduleResolution": "node",

and in functions/package.json added并在functions/package.json添加

"type": "module",

Now I can run the emulator without an issue.现在我可以毫无问题地运行模拟器了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firestore Angular 文档中的集合 - Firestore Angular Collection inside Document 如何使用 Angular 5 中的批量写入从 Cloud Firestore 中删除多个文档 - How to delete multiple documents from Cloud Firestore using Batched writes in angular 5 Firebase 云功能不更新 Firestore 数据库中的数据 - Firebase Cloud function not updating data in Firestore database 通过云运行时 Firestore 中的问题 function - Issue in the firestore while running through cloud function 调用 Firestore Cloud 函数超时 - Firestore Cloud Function Times Out When called 数据未通过调度程序 function 和 Firebase Cloud Functions 保存到 Firestore - Data not saved into Firestore through scheduler function with Firebase Cloud Functions 运行 Cloud Function 将 Firebase 经过身份验证的用户数据存储到 Firestore - Run Cloud Function to store Firebase Auth'ed user data to Firestore 使用云 function 和 firestore 更新文档后 1 小时运行一些代码 - run some code 1 hour after a document is updated with cloud function and firestore 触发云 function 后 Firestore DB 未更新? - Firestore DB not getting updated after firing cloud function? 使用 Cloud Functions 执行 Firestore 操作是否更安全? - Is it safer to do Firestore operations using Cloud Functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM