Skip to content

Cannot read properties of undefined (reading 'add') #1898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
zslucky opened this issue Jun 14, 2023 · 3 comments
Open

Cannot read properties of undefined (reading 'add') #1898

zslucky opened this issue Jun 14, 2023 · 3 comments

Comments

@zslucky
Copy link

zslucky commented Jun 14, 2023

protobuf.js version: 7.2.3

See related code bellow:

// Create the message type
var type = new Type(descriptor.name.length ? descriptor.name : "Type" + unnamedMessageIndex++, fromDescriptorOptions(descriptor.options, exports.MessageOptions)),
i;
/* Oneofs */ if (descriptor.oneofDecl)
for (i = 0; i < descriptor.oneofDecl.length; ++i)
type.add(OneOf.fromDescriptor(descriptor.oneofDecl[i]));
/* Fields */ if (descriptor.field)
for (i = 0; i < descriptor.field.length; ++i) {
var field = Field.fromDescriptor(descriptor.field[i], syntax);
type.add(field);
if (descriptor.field[i].hasOwnProperty("oneofIndex")) // eslint-disable-line no-prototype-builtins
type.oneofsArray[descriptor.field[i].oneofIndex].add(field);
}

Looks like Line 219 type.oneofsArray size may got 0, so type.oneofsArray[descriptor.field[i].oneofIndex] will return undefined, when I add && type.oneofsArray.lengh > 0 in Line 218, looks like work correctly. I'm not sure what's the correct logic here.

I used this repo behind @grpc/proto-loader, @grpc/grpc-jsand grpc-reflection-js, as I want to use reflection feature in my client.

So pls see my code below:

import * as grpc from '@grpc/grpc-js';
import * as protoLoader from '@grpc/proto-loader';
import * as grpcReflection from 'grpc-reflection-js';

const credentials = grpc.credentials.createInsecure();
const refclient = new grpcReflection.Client('127.0.0.1:9091', credentials);

const root = await refclient.fileContainingSymbol('some.symbol'); // some symbol in 127.0.0.1:9091
const descriptor = root.toDescriptor();
const descObj = protoLoader.loadFileDescriptorSetFromObject(descriptor, {});
const pkgObj = grpc.loadPackageDefinition(descObj);

Pls see tacktrace bellow:

TypeError: Cannot read properties of undefined (reading 'add')
    at Function.fromDescriptor (/Users/sh22888ml/demospace/delivery-svc-template/node_modules/protobufjs/ext/descriptor/index.js:219:66)
    at Function.fromDescriptor (/Users/sh22888ml/demospace/delivery-svc-template/node_modules/protobufjs/ext/descriptor/index.js:92:42)
    at createPackageDefinitionFromDescriptorSet (/Users/sh22888ml/demospace/delivery-svc-template/node_modules/@grpc/proto-loader/src/index.ts:345:60)
    at Object.loadFileDescriptorSetFromObject (/Users/sh22888ml/demospace/delivery-svc-template/node_modules/@grpc/proto-loader/src/index.ts:427:10)
    at Object.<anonymous> (/Users/sh22888ml/demospace/delivery-svc-template/src/modules/grpc-reflection/grpc-reflection.service.ts:15:33)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Promise.all (index 8)
    at GrpcReflectionSvc.onApplicationBootstrap (/Users/sh22888ml/demospace/delivery-svc-template/src/modules/grpc-reflection/grpc-reflection.service.ts:43:16)
    at async Promise.all (index 0)
    at callModuleBootstrapHook (/Users/sh22888ml/demospace/delivery-svc-template/node_modules/@nestjs/core/hooks/on-app-bootstrap.hook.js:43:5)
@cliffordfajardo
Copy link

I am also getting an error in the descriptor/index.js file as well:

if ((descriptor.oneofIndex = this.parent.oneofsArray.indexOf(this.partOf)) < 0)

but for a different method called oneofsArray:

/Users/cfajardo/Downloads/test-grpc/node_modules/protobufjs/ext/descriptor/index.js:489
            if ((descriptor.oneofIndex = this.parent.oneofsArray.indexOf(this.partOf)) < 0) {
                                                                 ^
TypeError: Cannot read properties of undefined (reading 'indexOf')
    at Field.toDescriptor (/Users/cfajardo/Downloads/test-grpc/node_modules/protobufjs/ext/descriptor/index.js:489:66)
    at Root_toDescriptorRecursive (/Users/cfajardo/Downloads/test-grpc/node_modules/protobufjs/ext/descriptor/index.js:142:40)
    at Root_toDescriptorRecursive (/Users/cfajardo/Downloads/test-grpc/node_modules/protobufjs/ext/descriptor/index.js:146:13)
    at Root.toDescriptor (/Users/cfajardo/Downloads/test-grpc/node_modules/protobufjs/ext/descriptor/index.js:121:5)
    at createPackageDefinition (/Users/cfajardo/Downloads/test-grpc/node_modules/@grpc/proto-loader/build/src/index.js:151:33)
    at Object.loadSync (/Users/cfajardo/Downloads/test-grpc/node_modules/@grpc/proto-loader/build/src/index.js:198:12)
    at file:///Users/cfajardo/Downloads/test-grpc/index.mjs:9:39
    at ModuleJob.run (node:internal/modules/esm/module_job:192:25)
    at async DefaultModuleLoader.import (node:internal/modules/esm/loader:228:24)
    at async loadESM (node:internal/process/esm_loader:40:7)

If I modify the source code and add a check around the existence of oneofsArray then my code works fine
I'm likely going to use patch-package to modify the source and add this check, while hopefully the bug fix is made.
Going to try and open a PR for oneofsArray check

@cliffordfajardo
Copy link

@zslucky - were you able to resolve your issue?

@zslucky
Copy link
Author

zslucky commented Oct 29, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants