Skip to content

Cannot read properties of undefined (reading 'indexOf') #1958

@justaguyin2k23

Description

@justaguyin2k23

protobuf.js version: 7.2.5
gprc-js version 1.9.13
proto-loader version 0.7.10

line #488 of [protobuf.js/ext/descriptor/index.js]

if ((descriptor.oneofIndex = this.parent.oneofsArray.indexOf(this.partOf)) < 0)
        throw Error("missing oneof");

causes the error Cannot read properties of undefined (reading 'indexOf') when calling proto-loader's loadSync function.

const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const pathLib = require('path');
const proto_path = [
  pathLib.join(__dirname,'./protos/dummy_service.proto')
];
 const packageDefinition = protoLoader.loadSync(protopath);

This specifically happens when the latest version of buf/validate.proto is among the proto files being used.

Changing line #488 to below solves the issue,

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

Stacktrace

From Node.js Internals:
  TypeError: Cannot read properties of undefined (reading 'indexOf')
      at Field.toDescriptor (/Users/tiredcoder/dev/cypress-tests/node_modules/protobufjs/ext/descriptor/index.js:488:62)
      at Root_toDescriptorRecursive (/Users/tiredcoder/dev/cypress-tests/node_modules/protobufjs/ext/descriptor/index.js:142:40)
      at Root_toDescriptorRecursive (/Users/tiredcoder/dev/cypress-tests/node_modules/protobufjs/ext/descriptor/index.js:146:13)
      at Root_toDescriptorRecursive (/Users/tiredcoder/dev/cypress-tests/node_modules/protobufjs/ext/descriptor/index.js:146:13)
      at Root.toDescriptor (/Users/tiredcoder/dev/cypress-tests/node_modules/protobufjs/ext/descriptor/index.js:121:5)
      at createPackageDefinition (/Users/tiredcoder/dev/cypress-tests/node_modules/@grpc/proto-loader/src/index.ts:327:66)
      at Object.loadSync (/Users/tiredcoder/dev/cypress-tests/node_modules/@grpc/proto-loader/src/index.ts:392:10)
 
     at createService (/Users/tiredcoder/dev/cypress-tests/cypress.config.ts:38:49)

I see a somewhat similar issue, #1898 but no resolution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions