John John - 6 months ago 185
Node.js Question

RangeError: Maximum call stack size exceeded for Node.js

I created model script in JS, so I can pass in an object and add in some logic and values. I use get/set to produce the values, however when i run my code it throws an RangeError: Maximum call stack size exceeded. Does anyone know who to fix this error?

var model = {
get id() {
return this.id ;
},
set id(id) {
this.id = id || '';
},
get filename(){
return this.filename ;
},
set filename(name){
this.filename = name || '';
},
get savename(){
return this.savename ;
},
set savename(name){
this.savename = name || '';
},
get filetype(){
return this.filetype ;
},
set filetype(name){
this.filetype = name || '';
}
};

module.exports = function(parm){
model.id = parm.id ;
model.filename = parm.filename ;
model.savename = parm.savename ;
modle.filetype = parm.filetype;
return model ;
};

Answer
get id() {
    return this.id ; 
},

You've defined a getter for the property called "id". Your implementation of the getter says that it should return the value of the property "id" from the object.

In order to get the value of the property "id", the system first checks to see if there's a getter for that property. In this case, there is, so the getter is called.

Same goes for the setter.

You have to keep the actual property values somewhere else than as values of the object property or else you'll run into exactly the problem you're having.

get id() {
  return this._id;
},
set id(value) {
  this._id = value || "";
},

You don't have to do it exactly like that; it's just a way that works.