Node JS + MongoDB + Mongoose RangeError:超出最大调用堆栈大小错误

时间:2016-12-28 04:35:53

标签: javascript node.js mongodb express

当我尝试从预定义模式创建一个新对象并将其插入我的mongodb数据库时,我一直收到一个RangeError。我希望有人可以帮我弄清楚为什么我遇到这个错误,希望能帮我解决这个错误,谢谢。

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>
&#13;
&#13;
&#13;

错误:

  

RangeError:超出最大调用堆栈大小           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 \ 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 \ 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)

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码

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);
  }
})
});