如何在NestJS中连接firestore数据库?

问题描述 投票:0回答:1

我创建了一个 firestore 数据库,我想使用我的 NestJS 后端 (Node.js ofc) 进行 CRUD 操作。我将 firebase 添加到 NestJS 中并且(我认为)正确设置了。我使用 SDK 管理服务帐户,firestore 数据库的规则设置为锁定。 每当我尝试测试连接(本地或生产中)时,firebase 应用程序都会成功初始化,但似乎集合/文档不存在,或者由于某种原因我无法连接到它。

详细信息。 我的 Nestjs package.json 的一部分(firebase 包在那里):

 "dependencies": {
    "@nestjs/common": "^10.0.0",
    "@nestjs/core": "^10.0.0",
    "@nestjs/jwt": "^10.2.0",
    "@nestjs/platform-express": "^10.0.0",
    "bcrypt": "^5.1.1",
    "firebase-admin": "^12.1.1",
    "reflect-metadata": "^0.2.0",
    "rxjs": "^7.8.1"
  }

我创建了一个 firebase 服务文件来初始化应用程序并在出现错误时注销:

import { Injectable, OnModuleInit } from '@nestjs/common';
import * as admin from 'firebase-admin';
import * as serviceAccount from 'myserviceaccount.json';

@Injectable()
export class FirebaseService implements OnModuleInit {
  private firestore: FirebaseFirestore.Firestore;

  async onModuleInit() {
    //checks if firestore is already initialized, if not, do so
    if (admin.apps.length === 0) {
      admin.initializeApp({
        credential: admin.credential.cert(
          serviceAccount as admin.ServiceAccount,
        ),
      });
      console.log('Firebase initialized successfully');
    } else {
      console.log('Firebase already initialized');
    }
    this.firestore = admin.firestore();

    //check if database is connected:
    try {
      const docRef = this.firestore.doc('mycollection/mydocument');
      const snapshot = await docRef.get();
      if (snapshot.exists) {
        console.log('Document data:', snapshot.data());
      } else {
        console.log('No such document!');
      }
    } catch (error) {
      console.error('Error fetching document:', error);
    }
  }

  getFirestore() {
    return this.firestore;
  }
}

这是进行增删改查操作的 NestJS 服务:

import { Injectable } from '@nestjs/common';
import { FirebaseService } from '../firebase.service';

@Injectable()
export class StampService {
  private firestore: FirebaseFirestore.Firestore;

  constructor(private readonly firebaseService: FirebaseService) {
    this.firestore = this.firebaseService.getFirestore();
  }
 
  async testFirestoreConnection(): Promise<any> {
    try {
      const docRef = this.firestore.doc('mycollection/mydocument')
      const snapshot = await docRef.get();
      console.log(snapshot.data().fieldname);
      return snapshot.data();
    } catch (error) {
      console.error('Firestore test error:', error);
      throw new Error('Firestore test failed');
    }
  }
}

当我运行“nest start watch”时,出现以下错误:

Firebase initialized successfully
Error fetching document: Error: 5 NOT_FOUND: 
    at callErrorFromStatus (path\node_modules\@grpc\grpc-js\src\call.ts:82:17)
    at Object.onReceiveStatus (path\node_modules\@grpc\grpc-js\src\client.ts:611:51)
    at Object.onReceiveStatus (path\node_modules\@grpc\grpc-js\src\client-interceptors.ts:419:48)
    at path\node_modules\@grpc\grpc-js\src\resolving-call.ts:163:24
    at processTicksAndRejections (node:internal/process/task_queues:77:11)
for call at
    at ServiceClientImpl.makeServerStreamRequest (path\node_modules\@grpc\grpc-js\src\client.ts:594:42)
    at ServiceClientImpl.<anonymous> (path\node_modules\@grpc\grpc-js\src\make-client.ts:189:15)
    at path\node_modules\@google-cloud\firestore\build\src\v1\firestore_client.js:237:29
    at path\node_modules\google-gax\build\src\streamingCalls\streamingApiCaller.js:38:28
    at path\node_modules\google-gax\build\src\normalCalls\timeout.js:44:16
    at Object.request (path\node_modules\google-gax\build\src\streamingCalls\streaming.js:377:40)
    at makeRequest (path\node_modules\retry-request\index.js:159:28)
    at retryRequest path\node_modules\retry-request\index.js:119:5)
    at StreamProxy.setStream (path\node_modules\google-gax\build\src\streamingCalls\streaming.js:368:37)
    at StreamingApiCaller.call (path\node_modules\google-gax\build\src\streamingCalls\streamingApiCaller.js:54:16)
Caused by: Error:
    at Firestore.getAll (path\node_modules\@google-cloud\firestore\build\src\index.js:1007:23)
    at DocumentReference.get (path\node_modules\@google-cloud\firestore\build\src\reference\document-reference.js:180:32)
    at FirebaseService.onModuleInit (path\src\firebase.service.ts:26:37)
    at MapIterator.iteratee (path\node_modules\@nestjs\core\hooks\on-module-init.hook.js:22:43)
    at MapIterator.next (path\node_modules\iterare\src\map.ts:9:39)
    at IteratorWithOperators.next (path\node_modules\iterare\src\iterate.ts:19:28)
    at Function.from (<anonymous>)
    at IteratorWithOperators.toArray (path\node_modules\iterare\src\iterate.ts:227:22)
    at callOperator (path\node_modules\@nestjs\core\hooks\on-module-init.hook.js:23:10)
    at callModuleInitHook (path\node_modules\@nestjs\core\hooks\on-module-init.hook.js:43:23) {
  code: 5,
  details: '',
  metadata: Metadata {
    internalRepr: Map(1) { 'x-debug-tracking-id' => [Array] },
    options: {}
  }
}

我真的无法尝试太多。我检查了 serviceaccount json 以查看它是否包含正确的服务帐户 ID 和正确的项目 ID。

我添加了控制台日志并尝试上面看到的 catch 块,甚至可以得到一些东西来看看问题是什么。

我确保我的收藏和文档参考准确无误,没有拼写错误。

我确保 FirebaseModule 在我的 app.module 中作为导入。

FirebaseModule 甚至被导入到 CRUD 服务所属的模块中。

我在NestJs中设置firebase/firestore的方式有问题吗?我在firebase控制台中设置的firestore数据库是否错误?难道是别的事?

如有任何帮助,我们将不胜感激!

javascript firebase google-cloud-firestore nestjs
1个回答
0
投票

我想我知道问题出在哪里。 firebase.service 的 onModuleInit 设置为异步,并且执行 CRUD 操作的 NestJS 服务方法也是异步的,因此 CRUD 方法可能比 firebase 服务中的 onModuleInit 完成得更快。

需要在 NestJS 服务方法中内置检查,以查看 firebase 应用程序是否已初始化,并仅在初始化时才让它执行 CRUD 操作。这解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.