Skip to content

Support casting JSON form of buffers #6863

@vkarpov15

Description

@vkarpov15

The below currently throws cast error:

const mongoose = require('mongoose');

mongoose.set('debug', true);

run();

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test');

  const Model = mongoose.model('Foo', new mongoose.Schema({
    buf: Buffer
  }));
  
  const doc = await Model.create({
    buf: {
      type: 'Buffer',
      data: [ 72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100 ] 
    }
  });
}

That's bad because the output JSON.stringify({ buf: Buffer.from('test') }); is '{"buf":{"type":"Buffer","data":[116,101,115,116]}}'

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementThis issue is a user-facing general improvement that doesn't fix a bug or add a new feature

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions