Skip to content

ObjectId Validation Check Fails To Convert To String #7364

@caleberi

Description

@caleberi

Context

While running queries in our staging environment, all database operations started failing due to a missing id attribute in the model definition. After debugging, we traced the issue to [lib/private/machines/private/process-native-record.js](https://github.com/balderdashy/sails-mongo/blob/master/lib/private/machines/private/process-native-record.js), which processes all incoming MongoDB ObjectId values.

The failure occurred during the validation stage, where the check for ObjectId instances returned false unexpectedly.

Debug Logs

Example of the logs we captured during the failure:

pkValue: new ObjectId("68a891f8c760e876123594df")
Primary key value: 68a891f8c760e876123594df
Primary key is Object: true
Primary key is instance of ObjectId: false

Sample Code

The relevant section of process-native-record.js:

Image

Root Cause

The pkValue instanceof ObjectId check is returning false even when the value is clearly an ObjectId. This prevents the adapter from converting the value into a string, ultimately causing the query to fail with a missing id attribute error.

Observed Error

Sails emitted the following warning when attempting to transform the results:

Warning: Records sent back from a database adapter should always have a valid property
that corresponds with the primary key attribute (`id`). But in this result set,
after transforming columnNames back to attribute names for model `business`,
there is a record with a missing or invalid `id`.

Record:
{
  id: new ObjectId('68a719aa320c7d7207c8cda8'),
  createdAt: '2025-08-21T13:05:46.205Z',
}

Next Steps

Further debugging shows this is likely due to different ObjectId references being used in validation (e.g., multiple mongodb driver instances loaded).

I’ve attached a sample PR to help clarify and resolve the issue:
🔗 [PR #502](balderdashy/sails-mongo#502)

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