2016-12-28 52 views
0

當我嘗試從預定義模式中創建新對象並將其插入到我的mongodb數據庫中時,我一直收到一個RangeError。我希望有人能幫我弄清楚爲什麼我遇到這個錯誤,並希望給我一個解決這個錯誤,謝謝。節點JS + MongoDB + Mongoose 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>

錯誤:

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\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)

回答

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); 
    } 
}) 
}); 
相關問題