2017-06-01 43 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

相關問題