XataX
Xata2y ago
4 replies
PeanutDumplings

Xata issues with environment variables in NestJS

I have a simple NestJS app, generated through their cli, and have attempted to add Xata to the project to use as my database. I've ran the
xata init
command, and everything generated correctly and my environment variables were added to my
.env
file. However, where I try to run my app with
pnpm start:dev
(a precreated script) I get this error:

E:\***\node_modules\.pnpm\@[email protected][email protected]\node_modules\@xata.io\client\src\client.ts:108
        throw new Error('Option apiKey is required');
              ^
Error: Option apiKey is required
    at XataClient.parseOptions_fn (E:\***\node_modules\.pnpm\@[email protected][email protected]\node_modules\@xata.io\client\src\client.ts:108:15)       
    at new _a (E:\***\node_modules\.pnpm\@[email protected][email protected]\node_modules\@xata.io\client\src\client.ts:47:27)
    at new XataClient (E:\***\src\lib\xata.ts:80:5)
    at getXataClient (E:\***\src\lib\xata.ts:89:14)
    at KeyCache.init (E:\***\remorse\nest-backend\src\lib\KeyCache.ts:12:40)
    at new KeyCache (E:\***\src\lib\KeyCache.ts:8:10)
    at Object.<anonymous> (E:\***\src\lib\KeyCache.ts:39:16)
    at Module._compile (node:internal/modules/cjs/loader:1378:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1437:10)
    at Module.load (node:internal/modules/cjs/loader:1212:32)


This is my
KeyCache.ts
file:

import { getXataClient } from './Xata';

class KeyCache {
  cache: Set<string>;

  constructor() {
    this.cache = new Set<string>();
    this.init();
  }

  async init() {
    const records = await getXataClient().db.uploads.select(['slug']).getAll();
    records.forEach((rec) => {
      if (rec && typeof rec.slug === 'string') {
        this.cache.add(rec.slug);
      }
    });
  }

  getUniqueId(length: number) {
    let string = this.generateRandomString(length);
    while (this.cache.has(string)) string = this.generateRandomString(length);
    this.cache.add(string);
    return string;
  }

  private generateRandomString(length: number) {
    let result = '';
    const characters =
      'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    for (let i = 0; i < length; i++)
      result += characters.charAt(
        Math.floor(Math.random() * characters.length),
      );
    return result;
  }
}

export default new KeyCache();


My environment variables are working fine as in the root file (
main.ts
) I have the following code:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';

async function bootstrap() {
  console.log(process.env.XATA_API_KEY);
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ValidationPipe());
  await app.listen(3000);
}
bootstrap();


and the appropriate variable is outputted near the start of the logs (before the errors).

Does anyone know what might be causing this error? Could it be the way in which files are loaded in NestJS, meaning that the KeyCache file that requires xata be loaded before environment variables are set/available to the app?

Thanks in advance :)
Was this page helpful?