scottwrobinson / camo

A class-based ES6 ODM for Mongo-like databases.
556 stars 80 forks source link

RangeError: Maximum call stack size exceeded when trying to write to non-existent field #112

Open Billydubb opened 6 years ago

Billydubb commented 6 years ago

Below is the full working example that leads to the following error. The problem is that company defines a field named employees but in the example I am trying to assign a value to employee. I was hoping that programmer errors like these would be part of the errors that the ORM/ODM would catch.

RangeError: Maximum call stack size exceeded
    at deepCopy (/Users/kelvin/node_modules/nedb/lib/model.js:128:22)
    at /Users/kelvin/node_modules/nedb/lib/model.js:130:18
    at Array.forEach (native)
    at deepCopy (/Users/kelvin/node_modules/nedb/lib/model.js:128:22)
    at /Users/kelvin/node_modules/nedb/lib/model.js:130:18
    at Array.forEach (native)
    at deepCopy (/Users/kelvin/node_modules/nedb/lib/model.js:128:22)
    at /Users/kelvin/node_modules/nedb/lib/model.js:122:41
    at Array.forEach (native)
    at deepCopy (/Users/kelvin/node_modules/nedb/lib/model.js:122:9)
var connect = require('camo').connect;
var Document = require('camo').Document;

var database;
var uri = 'nedb://companies';

class Company extends Document {
    constructor() {
        super();

        this.name = String;
        this.employees = [Employee];
    }

    static collectionName() {
        return 'companies';
    }
}

class Employee extends Document {
    constructor() {
        super();

        this.name = String;
        this.company = Company;
    }

    static collectionName() {
        return 'employees';
    }
}

connect(uri).then(function(db) {
    database = db;
    const comp = Company.create({name: 'Company inc.'});
    const employee = Employee.create({name: 'peter'});
    comp.employee = [employee];
    employee.company = comp;

    comp.save()
    .catch(err => {
        console.log(err);
    });
});
harrychiling commented 6 years ago

Your Employee class shoul extend the EmbeddedDocument class which needs to be pulled in.

//add this to top var EmbeddedDocument = require('camo').EmbeddedDocument;

//change this class Employee extends Document {

//to this class Employee extends EmbeddedDocument {

//change this var uri = 'nedb://companies';

//to this var uri ='nedb:///companies';

Im also not sure you can have circular references like the employee.company tryin to save an employee object inside a company object that has an array of an employee object is probably not going to work. Other than that your code looks good to me.