ai / nanoid

A tiny (124 bytes), secure, URL-friendly, unique string ID generator for JavaScript
https://zelark.github.io/nano-id-cc/
MIT License
24.66k stars 794 forks source link

Cannot read properties of undefined (reading 'length') #477

Closed maxwell-etz closed 6 months ago

maxwell-etz commented 6 months ago

I have been trying to use Nanoid in my Nestjs application. I keeps getting "Cannot read properties of undefined (reading 'length')".

TypeError: Cannot read properties of undefined (reading 'length') at customRandom (/home/maxwell/Documents/projects/js/project-damsyn/services/main/node_modules/nanoid/index.cjs:26:46) at customAlphabet (/home/maxwell/Documents/projects/js/project-damsyn/services/main/node_modules/nanoid/index.cjs:57:42) at new ProductOptionGroupService (/home/maxwell/Documents/projects/js/project-damsyn/services/main/src/product/product-option-group/product-option-group.service.ts:9:26) at Injector.instantiateClass (/home/maxwell/Documents/projects/js/project-damsyn/services/main/node_modules/@nestjs/core/injector/injector.js:365:19) at callback (/home/maxwell/Documents/projects/js/project-damsyn/services/main/node_modules/@nestjs/core/injector/injector.js:65:45) at Injector.resolveConstructorParams (/home/maxwell/Documents/projects/js/project-damsyn/services/main/node_modules/@nestjs/core/injector/injector.js:144:24) at Injector.loadInstance (/home/maxwell/Documents/projects/js/project-damsyn/services/main/node_modules/@nestjs/core/injector/injector.js:70:13) at Injector.loadProvider (/home/maxwell/Documents/projects/js/project-damsyn/services/main/node_modules/@nestjs/core/injector/injector.js:97:9) at /home/maxwell/Documents/projects/js/project-damsyn/services/main/node_modules/@nestjs/core/injector/instance-loader.js:56:13 at async Promise.all (index 4) at InstanceLoader.createInstancesOfProviders (/home/maxwell/Documents/projects/js/project-damsyn/services/main/node_modules/@nestjs/core/injector/instance-loader.js:55:9) at /home/maxwell/Documents/projects/js/project-damsyn/services/main/node_modules/@nestjs/core/injector/instance-loader.js:40:13 at async Promise.all (index 20) at InstanceLoader.createInstances (/home/maxwell/Documents/projects/js/project-damsyn/services/main/node_modules/@nestjs/core/injector/instance-loader.js:39:9) at InstanceLoader.createInstancesOfDependencies (/home/maxwell/Documents/projects/js/project-damsyn/services/main/node_modules/@nestjs/core/injector/instance-loader.js:22:13) at /home/maxwell/Documents/projects/js/project-damsyn/services/main/node_modules/@nestjs/core/nest-factory.js:108:17 at Function.asyncRun (/home/maxwell/Documents/projects/js/project-damsyn/services/main/node_modules/@nestjs/core/errors/exceptions-zone.js:22:13) at NestFactoryStatic.initialize (/home/maxwell/Documents/projects/js/project-damsyn/services/main/node_modules/@nestjs/core/nest-factory.js:106:13) at NestFactoryStatic.create (/home/maxwell/Documents/projects/js/project-damsyn/services/main/node_modules/@nestjs/core/nest-factory.js:42:9) at bootstrap (/home/maxwell/Documents/projects/js/project-damsyn/services/main/src/main.ts:8:15)

ai commented 6 months ago

Show me how do you call Nano ID?

maxwell-etz commented 6 months ago

import { customAlphabet } from 'nanoid' nanoid = customAlphabet('1234567890abcdef', 10) model.id = this.nanoid() //=> "4f90d13a42"

ai commented 6 months ago

Why it is this?

maxwell-etz commented 6 months ago

I am unable to use const in front nanoid when declaring. This is how I am using it in my project. nanoid version (3.0.0)

import { customAlphabet } from 'nanoid';

@Injectable()
export class ProductOptionGroupService {
  nanoid = customAlphabet(process.env.NANOID_GEN, 8);
  constructor(
    @InjectModel(ProductGroupOptions)
    private productModel: typeof ProductGroupOptions,
  ) {}

  async create(adminId: string, dto: CreateProductOptGrpDto) {
    const product = await this.productModel.create({
      adminId,
      name: dto.name,
      createdb: adminId,
      code: this.nanoid(),
    });
    return product;
  }
}
ai commented 6 months ago

More likely that process.env.NANOID_GEN is empty.

Error tells that alphabet is undefined.

maxwell-etz commented 6 months ago

Yeah. It worked. Thanks alot. I forgot to add NANOID_GEN to the env

ai commented 6 months ago

You can sponsor me here for helping debugging your commercial code

https://github.com/sponsors/ai