Skip to content

BSONError thrown when calling _id.toString() after using R.clone after mongoose bump #14353

@Axosoft-Ashley

Description

@Axosoft-Ashley

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.1.2

Node.js version

18

MongoDB server version

6.0

Typescript version (if applicable)

No response

Description

After bumping from v5.13.20 to 8.1.2, when R.clone() is called on an array of mongoose documents a BSON error is thrown, this did not happen on the older version of mongoose.
throw new BSONError("Cannot create Buffer from ${String(potentialBuffer)}");

Ramda version ^0.29.1 was used for both old and new mongoose test

Steps to Reproduce

schema setup

const userSchema = new mongoose.Schema({
  _id: { type: String, default: () => uuid.v4() },
});
const UserModel = mongoose.model('User', userSchema);

const organizationUser = {
  user: { type: String, ref: 'User' }
};

const organizationSchema = new mongoose.Schema({
  members: [organizationUser],
}, { usePushEach: true });

crash code:

    const R = require('ramda');
    const mongoose = require('mongoose');

    let foundOrgModel = await OrganizationModel.findOne({});
    if (!foundOrgModel) {
        // create user
        const user = await UserModel.create({})
        // create org
       foundOrgModel = await OrganizationModel.create({ members: [{ user: user._id }] })
    }

    foundOrgModel = await OrganizationModel.findOne({})
    const members = R.clone(foundOrgModel.members);
    console.log(members[0]._id.toString()) // crashes

Expected Behavior

it should not throw but instead just log the id as a string like it did in v5.13.20 of mongoose

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions