title: Why does it throw an error cannot read property of undefined (reading 'length') when running a GraphQL Subscription with Redis?
body: <p>Overview</p>
  <ul>
  <li>Working on a NestJS project with GraphQL using a laptop with Window OS</li>
  <li>Experimenting with GraphQL Subscriptions using <code>graphql-redis-subscription@2.5.0</code> package</li>
  <li>Redis is used in a docker container, see the <code>docker-compose.yml</code> below</li>
  <li>The problem arose when the subscription <code>postAdded</code> is executed in GraphQL Playground. Instead of hanging to listen for events, it had crashed before I performed <code>createPost</code> mutation.</li>
  </ul>
  <p>My code (I only include some important details)</p>
  <p>posts.resolver.ts</p>
  <pre class="lang-js prettyprint-override"><code>import { Inject, UseGuards } from '@nestjs/common';
  import { Args, Context, Mutation, Resolver, Subscription } from '@nestjs/graphql';
  import { RedisPubSub } from 'graphql-redis-subscriptions';
  import { GraphqlJwtAuthGuard } from '../auth/guards';
  import { RequestWithUser } from '../auth/interfaces';
  import { PUB_SUB } from '../pubsub/pubsub.module'
  
  const POST_ADDED_EVENT = 'postAdded';
  
  @Resolver(() =&gt; Post)
  export class PostsResolver {
    constructor(
      private postsService: PostsService,
      @Inject(PUB_SUB) private pubSub: RedisPubSub,
    ) {}
  
    // my subscription (issue)
    @Subscription(() =&gt; Post)
    postAdded() {
      return this.pubSub.asyncIterator(POST_ADDED_EVENT);
    }
  
    // createPost method
    @Mutation(() =&gt; Post)
    @UseGuards(GraphqlJwtAuthGuard)
    async createPost(
      @Args('input') createPostInput: CreatePostInput,
      @Context() context: { req: RequestWithUser },
    ) {
      // just create a new post (assuming it works)
      const newPost = await this.postsService.create(
        createPostInput,
        context.req.user,
      );
      this.pubSub.publish(POST_ADDED_EVENT, { postAdded: newPost });
      return newPost;
    }
  }
  </code></pre>
  <p>pubsub.module.ts</p>
  <pre class="lang-js prettyprint-override"><code>import { ConfigService } from '@nestjs/config';
  import { RedisPubSub } from 'graphql-redis-subscriptions';
  import { Global, Module } from '@nestjs/common';
  
  export const PUB_SUB = 'PUB_SUB';
  
  @Global()
  @Module({
    providers: [
      {
        provide: PUB_SUB,
        useFactory: (configService: ConfigService) =&gt;
          new RedisPubSub({
            connection: {
              host: configService.get('REDIS_HOST'),
              port: configService.get('REDIS_PORT'),
            },
          }),
        inject: [ConfigService],
      },
    ],
    exports: [PUB_SUB],
  })
  export class PubSubModule {}
  </code></pre>
  <p>app.module.ts</p>
  <pre class="lang-js prettyprint-override"><code>import { PubSubModule } from './pubsub/pubsub.module';
  
  @Module({
    imports: [
      GraphQLModule.forRoot&lt;ApolloDriverConfig&gt;({
        driver: ApolloDriver,
        playground: true,
        autoSchemaFile: path.join(process.cwd(), 'src/schema.gql'),
        installSubscriptionHandlers: true,
      }),
      PubSubModule,
      ConfigModule.forRoot({
        isGlobal: true,
        validationSchema: Joi.object({
          REDIS_HOST: Joi.string().required(),
          REDIS_PORT: Joi.number().required()
        }),
      }),
    ],
    providers: [AppService, AppResolver],
  })
  export class AppModule {}
  </code></pre>
  <pre class="lang-yaml prettyprint-override"><code>version: '3'
  services:
    redis:
      image: 'redis:alpine'
      ports:
        - '6379:6379'
  
    redis-commander:
      image: rediscommander/redis-commander:latest
      environment:
        - REDIS_HOSTS=local:redis:6379
      ports:
        - '8081:8081'
      depends_on:
        - redis
  </code></pre>
  <p>All the environment variables have already been defined in <code>.env</code> file.</p>
  <pre><code>REDIS_HOST=&quot;localhost&quot;
  REDIS_PORT=6379
  </code></pre>
  <p>When I run <code>yarn start:dev</code> and execute the subscription in GraphQL Playground</p>
  <pre><code>subscription {
    postAdded {
      id
      title
      paragraphs
    }
  }
  </code></pre>
  <p>it raises an error like this:</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;errors&quot;: [
      {
        &quot;message&quot;: &quot;Cannot read properties of undefined (reading 'length')&quot;,
        &quot;locations&quot;: [
          {
            &quot;line&quot;: 2,
            &quot;column&quot;: 3
          }
        ],
        &quot;path&quot;: [
          &quot;postAdded&quot;
        ]
      }
    ]
  }
  </code></pre>
  <p>The terminal that monitors NestJS also raises an error like this:</p>
  <pre><code>[Nest] 8080  - 07/21/2022, 9:30:24 AM   ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'length')
  TypeError: Cannot read properties of undefined (reading 'length')
      at JavascriptRedisParser.execute (C:\Users\HP\nestjs-project\node_modules\redis-parser\lib\parser.js:530:38)
      at Object.data (C:\Users\HP\nestjs-project\node_modules\ioredis\built\DataHandler.js:25:20)  
      at TransformOperationExecutor.transform (C:\Users\HP\nestjs-project\node_modules\src\TransformOperationExecutor.ts:207:39)
      at TransformOperationExecutor.transform (C:\Users\HP\nestjs-project\node_modules\src\TransformOperationExecutor.ts:327:31)
      at TransformOperationExecutor.transform (C:\Users\HP\nestjs-project\node_modules\src\TransformOperationExecutor.ts:327:31)
      at TransformOperationExecutor.transform (C:\Users\HP\nestjs-project\node_modules\src\TransformOperationExecutor.ts:327:31)
      at TransformOperationExecutor.transform (C:\Users\HP\nestjs-project\node_modules\src\TransformOperationExecutor.ts:327:31)
      at TransformOperationExecutor.transform (C:\Users\HP\nestjs-project\node_modules\src\TransformOperationExecutor.ts:327:31)
      at ClassTransformer.instanceToPlain (C:\Users\HP\nestjs-project\node_modules\src\ClassTransformer.ts:25:21)
      at Object.classToPlain (C:\Users\HP\nestjs-project\node_modules\src\index.ts:23:27)
  </code></pre>
  <p>I have installed all the necessary dependencies like <code>ioredis</code>, <code>graphql-redis-subscriptions</code> and even <code>graphql-subscriptions</code> but the errors still exist. Redis also seems to be running properly.</p>
  <p>I have tried reading the error logs but it did not occur in my source code and doing some research on StackOverFlow but none seems to have solved the problem.</p>