1

我正在使用 Angular 和 NestJS 构建一个 monorepo,并且很难弄清楚如何在 NestJS 应用程序中正确添加 Firestore。首先,我遇到的大部分事情都是让我们使用 Firebase 生成一个新的 NestJS 应用程序。我遇到了这个教程,感觉更接近我的情况,因为他们先构建了 Nest 应用程序,然后再添加 Firebase。但是我仍然迷路,因为它让我们删除了 firebase 安装的函数文件夹,然后告诉我们使用package.json在 Nest 应用程序中生成的文件。由于我使用 NRWL,该package.json文件是所有项目的根包文件,我不确定如何处理它。

我在index.ts它告诉我们使用此导入创建的文件中也出现错误

import * as functions from 'firebase-functions';

由于我的设置与他们的设置略有不同,它无法找到该文件或找出我应该将哪个文件放在它的位置。我没有任何特定的错误消息或发生任何事情,只是或多或少地试图弄清楚如何解决这个问题,并且无法找到围绕 NestJS 量身定制的更多信息。有人可以对此有所了解吗?

4

1 回答 1

2

第 1 步 - 创建 Nest 应用程序

nx generate @nrwl/nest:app myapp

第 2 步 - 添加功能(不在apps文件夹内)。

npm install -g firebase-tools
firebase init functions

删除函数目录。

rm -rf functions

现在更新 firebase 配置firebase.json以指向 Nest 应用程序。

{
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint",
      "npm --prefix \"$RESOURCE_DIR\" run build"
    ],
    "source": "."
  }
}

第 3 步 - 安装依赖项

npm i firebase-functions firebase-admin express @nestjs/platform-express

第 4 步 - 更新 package.json

将以下行添加到您的 package.json。

  "main": "dist/apps/myapp/main.js",
  "engines": {
    "node": "<=10"
  }

第 5 步 - 导出服务器

覆盖apps/myapp/src/main.ts.

import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import { AppModule } from './app/app.module';
import * as express from 'express';
import * as functions from 'firebase-functions';

const server = express();

export const createNestServer = async (expressInstance) => {
  const app = await NestFactory.create(
    AppModule,
    new ExpressAdapter(expressInstance),
  );

  return app.init();
};

createNestServer(server)
    .then(v => console.log('Nest Ready'))
    .catch(err => console.error('Nest broken', err));

export const api = functions.https.onRequest(server);

第 6 步 - 构建、服务、部署

nx build myapp && firebase serve --only functions
于 2020-05-08T11:35:24.343 回答