Skip to content

TypeError: type.startsWith is not a function when starting Parse Server with custom schema data #9847

@rgunindi

Description

@rgunindi

New Issue Checklist

Issue Description

When starting Parse Server with custom schema data, the server fails to start with the error:

TypeError: type.startsWith is not a function

The error originates from MongoSchemaCollection.js, specifically in the mongoFieldToParseSchemaField function.

Steps to reproduce

  1. Start Parse Server with custom MongoDB schema data(with incompatible data).
  2. Server crashes during initialization with the above error.

Actual Outcome

Parse Server crashes on startup with:

TypeError: type.startsWith is not a function
at mongoFieldToParseSchemaField (C:\Users\mypre\projects\parse-server-exp\node_modules\parse-server\lib\Adapters\Storage\Mongo\MongoSchemaCollection.js:25:12)
at mongoSchemaFieldsToParseSchemaFields (C:\Users\mypre\projects\parse-server-exp\node_modules\parse-server\lib\Adapters\Storage\Mongo\MongoSchemaCollection.js:78:29)
at mongoSchemaToParseSchema (C:\Users\mypre\projects\parse-server-exp\node_modules\parse-server\lib\Adapters\Storage\Mongo\MongoSchemaCollection.js:148:13)

Expected Outcome

Parse Server should handle unexpected or malformed schema type values gracefully instead of crashing.

Root Cause

The code calls type.startsWith('relation<') without ensuring that type is a string.
If type is undefined, null, or any non-string, startsWith is not available, causing a crash.

Problematic Code

// In MongoSchemaCollection.js
if (type.startsWith('relation<')) {
  return {
    type: 'Relation',
    targetClass: type.slice('relation<'.length, type.length - 1)
  };
}

Suggested Fix

if (!type || typeof type !== 'string') {
  throw new Parse.Error(...); // Ensure type is a string before calling startsWith
}

Environment
Server

Parse Server version: 6.2.0

Operating system: Windows 11

Local or remote host: Local

Database

System: MongoDB

Database version: 7.0

Local or remote host: Local

Logs
(see error stack trace above)

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:bugImpaired feature or lacking behavior that is likely assumed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions