_id:{type:Number} causes RangeError: Maximum call stack size exceeded #714

Closed
sjoerdmulder opened this Issue Feb 3, 2012 · 9 comments

Comments

Projects
None yet
2 participants

When you create a model it's fine, but when then after try to manipulate that model (remove or save method on instance). Node crashes with a RangeError: Maximum call stack size exceeded

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var db = mongoose.connect('mongodb://localhost/test');

var MySchema = new Schema({
    _id: { type: Number, unique: true },
    name: String
});

var MyModel = module.exports = mongoose.model('MyModel', MySchema);

//Autoincrement
MySchema.pre('save', function(next) {
    if (this.isNew || !this._id) {
        MyModel.find({},{_id:1}).sort('_id', -1).limit(1).find(function(err, items) {
            this._id = items.length ? items[0]._id + 1 : 1;
            next();
        }.bind(this));
    }
});

var instance = new MyModel({
    name: 'test'
});

instance.save(function(err, instance) {
    console.log(instance);
    //Crash!
    instance.remove();
});

Version 2.5.3
Node version: 0.6.9

aheckmann closed this in 746afb2 Feb 3, 2012

Collaborator

aheckmann commented Feb 3, 2012

fixed in 2.5.4

Thanks for the quick fix but it still crashes,

Actually use:

Model.findById(1, function(err, doc){
    doc.remove();
    //or
    doc.save();
})

Correction, now the behavior is that when you call save method, it never calls the callback function. And with the remove it crashes.

Collaborator

aheckmann commented Feb 3, 2012

ah, i was testing another issue. this fever is messing with me aasdfasdf. taking a look.

Collaborator

aheckmann commented Feb 3, 2012

ok 2.5.5 should work for you

Sorry to bother you again... :) remove works correctly now but save is actually never finish executing (or saving to the database) and so no callback is executing.. Object im trying to save:

{ _id: 2,
  application_id: 4f2b01b599e29f8d0400000a,
  version: 1,
  properties: [ { name: 'name', value: 'asd', _id: 4f2c5cd67ce3bd8135000003 } ],
  creationDate: Fri, 03 Feb 2012 22:13:16 GMT }

Take it easy on fixing though... I don't need this the coming weekend ;-)

Collaborator

aheckmann commented Feb 3, 2012

can't reproduce. can you open a new ticket?

Correct

I did open a ticket (as you might saw) but i closed it again when i realized it was my own Schema.pre('save' handler.

Thanks for the quick fix / release :D

Collaborator

aheckmann commented Feb 3, 2012

cool you're welcome!

@aheckmann aheckmann added a commit to aheckmann/mongoose that referenced this issue Feb 9, 2012

@aheckmann aheckmann fixed; RangeError when removing doc with Number _id
closes #714
0641b39

@aheckmann aheckmann added a commit to aheckmann/mongoose that referenced this issue Feb 23, 2012

@aheckmann aheckmann fix RangError: Maximum call stack exceeded error
closes #714
relates to #688
c639959

@aheckmann aheckmann added a commit to aheckmann/mongoose that referenced this issue Feb 23, 2012

@aheckmann aheckmann fixed; RangeError when removing doc with Number _id
closes #714
1094072
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment