balderdashy / balderdashy/sails

ObjectId Validation Check Fails To Convert To String

Open
#7,364 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
22.8k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

### 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)`](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](https://github.com/balderdashy/sails-mongo/pull/502)](https://github.com/balderdashy/sails-mongo/pull/502)

Contributor guide

Open the contributing guide

Research direction

Start with lib/private/machines/private/process-native-record.js, focusing on the ObjectId validation and string-conversion path described in the issue. Review PR #502 and reproduce the staging failure if possible; done means ObjectId values are recognized and converted so returned records retain a valid id.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.