简体   繁体   中英

Node JS + MongoDB + Mongoose RangeError: Maximum call stack size exceeded Error

I keep receiving a RangeError when I try to create a new object out of a predefined schema and insert it into my mongodb database. I was hoping someone could help me figure out why I am encountering this error and hopefully give me a solution to this error, thanks.

App.js

    var print = require('./print');
var port = 1338;
var express = require("express");
var app = express();
var bodyParser = require('body-parser');
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:1337/testdb';
var mongoose = require('mongoose')
var user = require('./schemas/user.js')
MongoClient.connect(url, function (err, db) {
  var collection = db.collection('users');
  if (err) {
    console.log('Unable to connect to the mongoDB server. Error:', err);
  } else {
    console.log('Connection established to', url);
  }




    app.use(bodyParser.urlencoded({ extended: true }));
    app.set('view engine','ejs');
    app.get('/', function(req,res){
          res.render("post");
          print("All is in order");
    })





    app.post('/post', function(req, res) {
      var name = req.body.name
      var email = req.body.email
      res.send(`You sent the name ${name} and the email ${email}`);
      var user1 = new user()
      user1.name = name
      user1.email = email
      collection.insert(user1, function(err,result){
      if (err) {
        console.log(err);
      } else {
        console.log('The Results', result);
      }
    })
  });


    app.listen(port,function(){
      print(`listening on ${port}`);
    })

})

User.js

 var mongoose = require('mongoose');
var Schema = mongoose.Schema
var UserSchema = new Schema({

  name: {type: String},
  email:{type: String}

});
module.exports = mongoose.model('User', UserSchema);

Post.ejs

 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>CSS3 Contact Form</title> </head> <body> <div id="contact"> <h1>Send an email</h1> <form action="http://127.0.0.1:1338/post" method="post"> <fieldset> <label for="name">Name:</label> <input type="text" id="name" name="name" placeholder="Enter your full name" /> <label for="email">email:</label> <input type="email" id="email" name="email" placeholder="Enter your email" /> <input type="submit" value="Send message" /> </fieldset> </form> </div> </body> </html> 

Error:

RangeError: Maximum call stack size exceeded at calculateElement (C:\\Users\\Programming\\Documents\\Web_Repo\\node_modules\\bson\\lib\\bson\\parser\\calculate_size.js:49:26) at calculateObjectSize (C:\\Users\\Programming\\Documents\\Web_Repo\\node_modules\\bson\\lib\\bson\\parser\\calculate_size.js:38:22) at calculateElement (C:\\Users\\Programming\\Documents\\Web_Repo\\node_modules\\bson\\lib\\bson\\parser\\calculate_size.js:123:77) at calculateObjectSize (C:\\Users\\Programming\\Documents\\Web_Repo\\node_modules\\bson\\lib\\bson\\parser\\calculate_size.js:28:22) at calculateElement (C:\\Users\\Programming\\Documents\\Web_Repo\\node_modules\\bson\\lib\\bson\\parser\\calculate_size.js:123:77) at calculateObjectSize (C:\\Users\\Programming\\Documents\\Web_Repo\\node_modules\\bson\\lib\\bson\\parser\\calculate_size.js:38:22) at calculateElement (C:\\Users\\Programming\\Documents\\Web_Repo\\node_modules\\bson\\lib\\bson\\parser\\calculate_size.js:123:77) at calculateObjectSize (C:\\Users\\Programming\\Documents\\Web_Repo\\node_modules\\bson\\lib\\bson\\parser\\calcu late_size.js:38:22) at calculateElement (C:\\Users\\Programming\\Documents\\Web_Repo\\node_modules\\bson\\lib\\bson\\parser\\calculate_size.js:123:77) at calculateObjectSize (C:\\Users\\Programming\\Documents\\Web_Repo\\node_modules\\bson\\lib\\bson\\parser\\calculate_size.js:28:22) at calculateElement (C:\\Users\\Programming\\Documents\\Web_Repo\\node_modules\\bson\\lib\\bson\\parser\\calculate_size.js:123:77) at calculateObjectSize (C:\\Users\\Programming\\Documents\\Web_Repo\\node_modules\\bson\\lib\\bson\\parser\\calculate_size.js:38:22) at calculateElement (C:\\Users\\Programming\\Documents\\Web_Repo\\node_modules\\bson\\lib\\bson\\parser\\calculate_size.js:123:77) at calculateObjectSize (C:\\Users\\Programming\\Documents\\Web_Repo\\node_modules\\bson\\lib\\bson\\parser\\calculate_size.js:38:22) at calculateElement (C:\\Users\\Programming\\Documents\\Web_Repo\\node_modules\\bson\\lib\\bson\\parser\\calculate_size.js:123:77) at calculateObjectSize (C:\\Users\\Programming\\Documents\\Web_Repo\\node_modules\\bson\\lib\\bson\\parser\\calculate_size.js:28:22)

Try to use below code

 app.post('/post', function(req, res) { var name = req.body.name var email = req.body.email res.send(`You sent the name ${name} and the email ${email}`); var user1 = {}; user1.name = name; user1.email = email; collection.insert(user1, function(err,result){ if (err) { console.log(err); } else { console.log('The Results', result); } }) }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM