Skip to content

Array default bug #5780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SteveCruise opened this issue Nov 2, 2017 · 4 comments
Closed

Array default bug #5780

SteveCruise opened this issue Nov 2, 2017 · 4 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@SteveCruise
Copy link

bug

What is the current behavior?

I set like this.

permissions: {
        type: [[Number]],
        default: [[0, 1]]
    },

But the result default is this:

permissions: [ [ 0 ], [ 1 ] ],

I want this:

permissions: [ [ 0, 1 ] ],

node.js mongoose and MongoDB is the latest version.

@vkarpov15 vkarpov15 added this to the 4.13.1 milestone Nov 2, 2017
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Nov 2, 2017
@vkarpov15
Copy link
Collaborator

Thanks for reporting, will investigate ASAP

@sobafuchs
Copy link
Contributor

yup this looks like a bug:

const mongoose = require('mongoose');
const co = require('co');
mongoose.Promise = global.Promise;
const GITHUB_ISSUE = `gh-5780`;


exec()
  .then(() => {
    console.log('successfully ran program');
    process.exit(0);
  })
  .catch(error => {
    console.error(`Error: ${error}\n${error.stack}`);
  });


function exec() {
  return co(function* () {
    yield mongoose.connect(`mongodb://localhost:27017/${GITHUB_ISSUE}`, { useMongoClient: true })
    const schema = new mongoose.Schema({
      permissions: {
        type: [[Number]],
        default: [[0, 1]]
      },
    });

    const Model = mongoose.model('Model', schema);

    console.log(new Model({ }));
  });
}

the log output:

{ _id: 59fbded47a74f264def9f1a6, permissions: [ [ 0 ], [ 1 ] ] }

@sobafuchs sobafuchs added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Nov 3, 2017
@vkarpov15
Copy link
Collaborator

As a workaround do this:

    const schema = new mongoose.Schema({
      permissions: {
        type: [[Number]],
        default: () => [[0, 1]]
      },
    });

Something is wrong with the way nested array defaults get casted, but using a function rather than an array as the default seems to avoid this issue. We'll fix this issue ASAP but till then just use the workaround 👍

vkarpov15 added a commit that referenced this issue Nov 4, 2017
@SteveCruise
Copy link
Author

SteveCruise commented Nov 6, 2017

@vkarpov15 Thank you very much!!! Great!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants