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

RangeError: Maximum call stack size exceeded while updating data to the database after adding jsonwebtoken in url #6214

Closed
rohan-paul opened this issue Mar 7, 2018 · 3 comments

Comments

@rohan-paul
Copy link

rohan-paul commented Mar 7, 2018

In my App (A messenger app, when I enter in the new message, in the message (string) input, and click on submit, the node server closes giving me the below error
Although the new message that I am submitting in the in the input form gets saved in the database. And when I restart the node server, it shows up.

/home/paul/codes/ng2/Seed-MEAN-Feb18-WIP/node_modules/mongodb/lib/utils.js:132
      throw err;
      ^

RangeError: Maximum call stack size exceeded
    at model.Document.$toObject (/home/paul/codes/ng2/Seed-MEAN-Feb18-WIP/node_modules/mongoose/lib/document.js:2058:40)
    at model.Document.toJSON (/home/paul/codes/ng2/Seed-MEAN-Feb18-WIP/node_modules/mongoose/lib/document.js:2401:15)
    at clone (/home/paul/codes/ng2/Seed-MEAN-Feb18-WIP/node_modules/mongoose/lib/utils.js:164:18)
    at cloneObject (/home/paul/codes/ng2/Seed-MEAN-Feb18-WIP/node_modules/mongoose/lib/utils.js:246:11)
    at clone (/home/paul/codes/ng2/Seed-MEAN-Feb18-WIP/node_modules/mongoose/lib/utils.js:172:16)
    at model.Document.$toObject (/home/paul/codes/ng2/Seed-MEAN-Feb18-WIP/node_modules/mongoose/lib/document.js:2109:13)
    at model.Document.toJSON (/home/paul/codes/ng2/Seed-MEAN-Feb18-WIP/node_modules/mongoose/lib/document.js:2401:15)
    at clone (/home/paul/codes/ng2/Seed-MEAN-Feb18-WIP/node_modules/mongoose/lib/utils.js:164:18)
    at cloneArray (/home/paul/codes/ng2/Seed-MEAN-Feb18-WIP/node_modules/mongoose/lib/utils.js:260:14)
    at clone (/home/paul/codes/ng2/Seed-MEAN-Feb18-WIP/node_modules/mongoose/lib/utils.js:159:12)
    at cloneObject (/home/paul/codes/ng2/Seed-MEAN-Feb18-WIP/node_modules/mongoose/lib/utils.js:246:11)
    at clone (/home/paul/codes/ng2/Seed-MEAN-Feb18-WIP/node_modules/mongoose/lib/utils.js:172:16)
    at model.Document.$toObject (/home/paul/codes/ng2/Seed-MEAN-Feb18-WIP/node_modules/mongoose/lib/document.js:2109:13)
    at model.Document.toJSON (/home/paul/codes/ng2/Seed-MEAN-Feb18-WIP/node_modules/mongoose/lib/document.js:2401:15)
    at clone (/home/paul/codes/ng2/Seed-MEAN-Feb18-WIP/node_modules/mongoose/lib/utils.js:164:18)
    at cloneObject (/home/paul/codes/ng2/Seed-MEAN-Feb18-WIP/node_modules/mongoose/lib/utils.js:246:11)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! udemy-nodejs-angular2@1.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the udemy-nodejs-angular2@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/paul/.npm/_logs/2018-03-07T12_32_54_826Z-debug.log

The content in my ../models/messages.js

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

var User = require('./user');

var schema = new Schema({
    content: {type: String, required: true},
    user: {type: Schema.Types.ObjectId, ref: 'User'}
});

schema.post('remove', function (message) {
    User.findById(message.user, function (err, user) {
        user.messages.pull(message);
        user.save();
    });
});

module.exports = mongoose.model('Message', schema);

This issue started happening after I inserted the code for jsonwebtoken in ./message/message.service.ts file for adding the token to my post url

addMessage(message: Message) {
        const body = JSON.stringify(message);
        const headers = new Headers({'Content-Type': 'application/json'});
        const token = localStorage.getItem('token')
            ? '?token=' + localStorage.getItem('token')
            : '';
        return this.http.post('http://localhost:3000/message' + token, body, {headers: headers})
            .map((response: Response) => {
                const result = response.json();
                const message = new Message(
                    result.obj.content,
                    result.obj.user.firstName,
                    result.obj._id,
                    result.obj.user._id);
                this.messages.push(message);
                return message;
            })
            .catch((error: Response) => Observable.throw(error.json()));
    }

The full source code of the project - Full code base

Please mention your node.js, mongoose and MongoDB version.
node - v9.3.0
mongodb - version v3.6.3
mongoose - 5.0.9

@rohan-paul rohan-paul changed the title RangeError: Maximum call stack size exceeded while updating data to the database RangeError: Maximum call stack size exceeded while updating data to the database after adding jsonwebtoken in url Mar 7, 2018
@lineus
Copy link
Collaborator

lineus commented Mar 7, 2018

Hi @rohan-paul,

thanks for making this easy to trouble shoot. I cloned your repo and there are 2 lines that need to be changed to make it work:

InspiredMacPro:rohan lineus$ git diff routes/messages.js 
diff --git a/routes/messages.js b/routes/messages.js
index 21996e0..5e3360d 100644
--- a/routes/messages.js
+++ b/routes/messages.js
@@ -45,7 +45,7 @@ router.post('/', function (req, res, next) {
         }
         var message = new Message({
             content: req.body.content,
-            user: user
+            user: user._id
         });
         message.save(function (err, result) {
             if (err) {
@@ -55,7 +55,7 @@ router.post('/', function (req, res, next) {
                 });
             }
             // Now because, I am connecting user with message, I have to save messages array in user model.
-            user.messages.push(result);
+            user.messages.push(result._id);
             user.save();
             res.status(201).json({
                 message: 'Saved message',

after making these changes* I was able to add messages without it crashing.
I don't have any meaningful experience with ts, and I got an error on npm install about native compiling some of the ts packages, so I just edited the js without digging into ts. hope this helps.

@lineus
Copy link
Collaborator

lineus commented Mar 7, 2018

show dbs
admin 0.078GB
config 0.078GB
local 0.078GB
mongoose_test 0.203GB
node-angular 0.078GB
test 0.078GB
use node-angular
switched to db node-angular
show collections
messages
system.indexes
users
db.users.find().pretty()
{
"_id" : ObjectId("5a9ff3800f2abba5379e8e93"),
"firstName" : "flippy",
"lastName" : "mcdoogal",
"password" : "$2a$10$J5H/BEKxik2SOt1b/vxWgOQIdNEERynejS7n6okj6JHTfhO/DySSG",
"email" : "flippy@mc.du",
"messages" : [
ObjectId("5a9ff4fe079501a615a5f6ff"),
ObjectId("5a9ff5d654a45ba796525206")
],
"__v" : 2
}

@rohan-paul
Copy link
Author

Hi @lineus - That works great.. thanks so very much..

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