Skip to content
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

Cannot read property 'documentClass' of undefined #35

Closed
michaeljota opened this issue Jan 17, 2016 · 8 comments
Closed

Cannot read property 'documentClass' of undefined #35

michaeljota opened this issue Jan 17, 2016 · 8 comments

Comments

@michaeljota
Copy link

I'm doing an API RESTful with camo using nedb as datastorage. The problem I'm facing is, when I update an array inside another array, I can't access that document anymore.

If I restart the server and load the datastorage again, I can access the data again, even the updated one, so it does saved it. How ever, I doesn't seem to be able to read it after.

Here's my code:

To update:

 Classroom.loadOne({_id: req.params.classId})
        .then(function(classroom){
            var shape = Shape.create({
                type: req.body.type
            });
            classroom.whiteboards[classroom.whiteboards.length-1].shapes.push(shape);
            classroom.save()
                .then(classroom => successHandler (res, shape))
                .catch(err => errorHandler (res, err));
        })
        .catch(err => errorHandler(res, err))

To read:

Classroom.loadOne({_id: req.params.classId})
        .then(classroom => successHandler (res, classroom))
        .catch(err => errorHandler (res, err));

successHandler and errorHandler, just send a response with 200 or 500 status, and jsonp of the document or the error.

Hope can help.

Thanks!

@michaeljota
Copy link
Author

Somehow, the document still open after the save() function.

update codes:

    addWhiteboard () {
        this._indexWb = this.whiteboards.push(Whiteboard.create());
    };

    addShape (data) {
        var shape = Shape.create({
            type: data.type
        });
        this._indexShape = this.whiteboards[this._indexWb-1].shapes.push(shape);
    };

    getShape () {
        return this.whiteboards[this._indexWb-1].shapes[this._indexShape-1].toJSON();
    }

    addPoint (point) {
        this.whiteboards[this._indexWb-1].shapes[this._indexShape-1].points.push(Point.create({
            x: point.x,
            y: point.y
        }))
    }

Now they are part of the document.

Am I doing something wrong?

@michaeljota
Copy link
Author

I am, at this time, not able to update none of the index. Even if I set them in those function, there are resetting.

@scottwrobinson
Copy link
Owner

Hi Michael,

A couple quick notes:

  • Make sure you're calling .save() on all updated objects and all new objects created with .create(). I see in the code above that there are a few objects created (Whiteboard, Shape, Point) that don't have .save() called
  • Calling .save() on an object does not call .save() on its referenced objects. Every new or updated object needs to have .save() called explicitly

When you say "The problem I'm facing is, when I update an array inside another array, I can't access that document anymore", do you mean this line?

classroom.whiteboards[classroom.whiteboards.length-1].shapes.push(shape);

I'll do some testing and see if I can find any problems. It might be helpful if you could post the code for the Classroom and Whiteboard classes. You don't have to post all of it, at least the relevant parts of the schema.

Thanks.

@michaeljota
Copy link
Author

Whiteboard, Shape and Point are EmbeddedDocument. I tried to save them, but it throws "save()" it's undefined.

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

        this.className = {
            type: String,
            required: true
        };

        this.date = {
            type: Date,
            default: Date.now()
        };

        this.whiteboards = {
            type: [Whiteboard],
            default: [Whiteboard.create()]
        };

        this._indexWb = 0;
        this._indexShape = -1;
    }

    get indexWb () {
        return this._indexShape;
    }

    set indexWb (value) {
        console.log('indexWb: '+value);
        this._indexShape = value;
    }

    get indexShape() {
        return this._indexShape;
    }

    set indexShape(value) {
        console.log('indexShape: '+value);
        this._indexShape = value;
    }

    addWhiteboard () {
        this.indexWb = this.whiteboards.push(Whiteboard.create());
    };

    addShape (data) {
        var shape = Shape.create({
            type: data.type
        });
        this.indexShape = this.whiteboards[this.indexWb].shapes.push(shape);
    };

    getShape () {
        return this.whiteboards[this.indexWb].shapes[this.indexShape].toJSON();
    }

    addPoint (point) {
        this.whiteboards[this.indexWb].shapes[this.indexShape].points.push(Point.create({
            x: point.x,
            y: point.y
        }))
    }
}

This is the current class of the Classroom document. I change things to test. There is no way to update either of the _index, not even with the index get and set (BTW, because of the getters and setters being publics, now the document save them).

I will just move on and use standard Document. I think that may solve the problem. I'll let you know. Thanks!

@scottwrobinson
Copy link
Owner

Ah, okay, I didn't realize Whiteboard, Shape and Point were EmbeddedDocuments. That makes a lot more sense.

I think you may have found an issue with saving nested arrays, so I'll look in to it.

Thanks!

@michaeljota
Copy link
Author

I think it maybe related to a nested arrays of EmbeddedDocuments. I'm unable right now to test it. But next week or so, I'll help you properly reporting a playable scenario.

@scottwrobinson
Copy link
Owner

Found the issue. You're right, there is a problem saving/loading deeply nested EmbeddedDocuments. I'll be committing the fix shortly.

Thanks for pointing this out!

@michaeljota
Copy link
Author

Glad I could help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants