2017-06-01 45 views
-1

我试图将表单数据提交到mongoDB数据库。但我很难做到这一点。获取“TypeError:无法在我的index.js文件中读取未定义的”错误集合“ 我的表单页面如下。nodejs/mongoDB - TypeError:无法读取未定义的属性“集合”

<form class = "profile" action = "/process" method="post"> 
<div class="form-group"> 
<label for="Fname">First Name: </label> 
<input type="text" id="fName" name = "fName" class="form-control" placeholder="first name" > 
</div> 
<div class="form-group"> 
<label for="Lname">Last Name: </label> 
<input type="text" id="lName" name = "lName" class="form-control" placeholder="last name" > 
</div> 
<div class="form-group"> 
<label for="email">Email : </label> 
<input type="email" id="email" name = "email" class="form-control" placeholder="email.." required> 
</div> 
<div class="form-group"> 
<input type="radio" name="gender" value="male" checked> Male<br> 
<input type="radio" name="gender" value="female"> Female<br> 
</div> 
<button type="submit" class="btn btn-primary" id="save">Submit</button> 
</form> 

的index.js文件

var express = require('express'); 
var path = require('path'); 
var bodyParser = require('body-parser'); 
var mongoose = require('mongoose'); 
const config = require('./config/database'); 
var routes = require('./routes/main'); 
var db = mongo.db('localhost:27017/appointment'); 

mongoose.connect(config.database); 
//check connection 
mongoose.connection.on("connected",function(database){ 
db = database; 
console.log('connected to db',+config.database); 
}); 
var index = express(); 
//declare port 
const port = 3000; 

index.post('/process',function(req,res){ 
db.collection('appoint').save({firstName : req.body.fName, 
lastName : req.body.lName, 
email : req.body.email, 
phone : req.body.phone, 
dob : req.body.dob, 
gender : req.body.gender}, function(err,result){ 
    if(err) { throw err; } 
     console.log("saved to database"); 
     res.redirect('/thanks'); 

}); 
}); 
//connect to port 
index.listen(port,function(){ 
console.log("listening to port ",port); 
}); 

请让我知道我在做什么错。我被困在同一个地方一段时间。

问候, 扬

回答

0

看来你的错误是说db是不确定的,所以当它试图从db.collection,得到.collection它不知道哪个collection获得。

您是否试过将var db = mongo.db('localhost:27017/appointment');更改为var db = mongo.db('localhost:27017');db.collection('appoint')db.collection('appointment')

此外,在mongoose.connect(config.database);你可以检查是否有一个错误:

mongoose.connect(config.database, (error) => { 
    if(error) console.error(error); 
    else console.log('connected to db ' + config.database); 
}); 

希望这些帮助。

+0

感谢ervi的回复。我根据您的建议更改了代码。但仍然存在相同的错误。 :( – janani

+0

你看到从console.error打印出来的错误(错误);或者你看到“连接到db ...”输出来自'console.log'吗? –

+0

它显示连接到db – janani

0

检查下面的代码,代码中的回调函数返回错误和响应,而不是数据库连接。连接后,您需要使用您用于连接的var来获取集合或创建新对象。

var express = require('express'); 
var path = require('path'); 
var bodyParser = require('body-parser'); 
var mongoose = require('mongoose'); 
const config = require('./config/database'); 
var routes = require('./routes/main'); 
var db = mongo.db('localhost:27017/appointment'); 

mongoose.connect(config.database); 
//check connection 
mongoose.connection.on("connected",function(database){ 
    console.log('connected to db',+config.database); 
}); 

var index = express(); 
//declare port 
const port = 3000; 

index.post('/process',function(req,res){ 
    mongoose.collection('appoint').save({firstName : req.body.fName, 
    lastName : req.body.lName, 
    email : req.body.email, 
    phone : req.body.phone, 
    dob : req.body.dob, 
    gender : req.body.gender}, function(err,result){ 
     if(err) { throw err; } 
      console.log("saved to database"); 
      res.redirect('/thanks'); 

     }); 
}); 
//connect to port 
index.listen(port,function(){ 
    console.log("listening to port ",port); 
}); 
+0

感谢您的回复。但它再次抛出相同的错误 – janani

相关问题