SoftTimur
SoftTimur

Reputation: 5540

Populate before adding: RangeError: Maximum call stack size exceeded

I want to realise an online editor like plunker. I have defined the following data structure: a post (ie, a project) contains a list of folders (which means different versions of the post), and a folder is just a list of files.

var PostSchema = new mongoose.Schema({
  folders: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Folder' }]
  ...
});

var FolderSchema = new mongoose.Schema({
    files: [{ name: String, body: String, editorOption: Object }],
    post: { type: mongoose.Schema.Types.ObjectId, ref: 'Post' }
})

I want to use the following request to add a folder/version (by the save button on the editor). Note that, because a post can be opened in different places at the same time, we have to fetch the post from the database before adding a version to it, then we return the up-to-date post to the controller.

o.addFolder = function (id, folder) {
    return $http.post('/posts/' + id + '/editor/folders', folder, {
        headers: { Authorization: 'Bearer ' + auth.getToken() }
    })
}

router.post('/posts/:post/editor/folders', auth, function (req, res, next) {
    req.post.populate('folders', function (err, post) {
        if (err) return next(err);
        var folder = new Folder(req.body);
        folder.post = post;
        folder.save(function (err, folder) {
            if (err) return next(err);
            console.log("3 folder: "); console.log(folder);
            console.log("4 post: "); console.log(post);
            post.folders.push(folder);
            console.log("5 folder: "); console.log(folder);
            console.log("6 post: "); console.log(post);
            post.save(function (err, post) {
                if (err) return next(err);
                res.json({ folder: folder, post: post })
            })
        })
    })
});

Here is a test log which tries to add a folder of 2 files to an empty post:

3 folder: 
{ __v: 0,
  post: 
   { _id: 58b5e86d8e328f58f3e87ac2,
     __v: 0,
     folders: [] },
  _id: 58b5e86d8e328f58f3e87ac3,
  files: 
   [ { name: 'index.html',
       body: '<!DOCTYPE html>\n<body>\nindex.html\n</body>\n</html>',
       editorOption: [Object],
       _id: 58b5e86d8e328f58f3e87ac5 },
     { name: 'script.js',
       body: '',
       editorOption: [Object],
       _id: 58b5e86d8e328f58f3e87ac4 } ] }
4 post: 
{ _id: 58b5e86d8e328f58f3e87ac2,
  __v: 0,
  folders: [] }
5 folder: 
events.js:85
      throw er; // Unhandled 'error' event
            ^
RangeError: Maximum call stack size exceeded

Does anyone know what's wrong there?

Upvotes: 1

Views: 167

Answers (1)

SoftTimur
SoftTimur

Reputation: 5540

The following code accomplishes what I want. Apparently, req.post.folders.push(folder) can push folder to the up-to-date post. And we do populate in the end.

router.post('/posts/:post/editor/folders', auth, function (req, res, next) {
    var folder = new Folder(req.body);
    folder.post = req.post;
    folder.save(function (err, folder) {
        if (err) return next(err);
        req.post.folders.push(folder);
        req.post.save(function (err, post) {
            if (err) return next(err);
            req.post.populate('folders', function (err, post) {
                if (err) return next(err);
                res.json(post);
            })
        })
    })
});

Upvotes: 1

Related Questions