tylim88/Firelord

regeneratorRuntime is not defined

Closed this issue · 2 comments

Hey, I'm trying to use your project in a NestJS application. However when I try to use I get this error:

ReferenceError: regeneratorRuntime is not defined
at /.../nestjs-firebase/node_modules/firelord/dist/docCreator.js:68:41
at Object.doc (/.../nestjs-firebase/node_modules/firelord/dist/docCreator.js:88:8)

The code looks like this:

import { Injectable } from '@nestjs/common';
import { firestore } from 'firebase-admin';
import { firelord, FirelordUtils } from 'firelord';
import { OmitKeys } from 'firelord/dist/firelordUtils';
import { Wrapper } from 'firelord/dist/index_';

@Injectable()
export class BaseRepository<
  T extends FirelordUtils.MetaType,
  N extends T['colName'] = T['colName'],
> {
  private creator: Wrapper<T>;
  private collection: ReturnType<Wrapper<T>['col']>;
  /**
   *
   */
  constructor(
    private readonly collectionName: N,
    private fs: typeof firestore,
  ) {
    const { wrapper } = firelord(fs);
    this.creator = wrapper<T>();
    this.collection = this.creator.col(collectionName);
  }

  getById(id: T['docID']) {
    return this.collection.doc(id).get();
  }

  insert(data: OmitKeys<T['writeNested'], 'updatedAt' | 'createdAt'>) {
    return this.collection.doc().create(data);
  }
}

It doesn't matter which of the functions is being called (getById or insert).

The type I'm trying this with is:

import { FirelordUtils } from 'firelord';

export type User = FirelordUtils.ReadWriteCreator<
  {
    name: string;
  },
  'users',
  string
>;

My tsconfig looks like this:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017", // I have also tried es5 (no luck)
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "strict": true,
    "incremental": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": false,
    "resolveJsonModule": true,
}

The firestore instance is correctly instantiated.

normally we can use async await in nodejs out of the box

however, under the hood Typescript / babel compile async await in Firelord into generator

that is why you have this error, the solution however is very simple

first install regenerator runtime

npm i regenerator-runtime

then in the very 1st line of code, add this line

import 'regenerator-runtime/runtime'

you only need to add this line once

Wow. I cannot believe I missed this line import 'regenerator-runtime/runtime' in the docs...

Thanks for quickly helping and sorry for the inconvenience :)