我正在尝试将表单数据提交到mongoDB数据库中。但我很难做到。在我的索引文件中收到"类型错误:无法读取未定义的属性'集合'"错误.js 我的表单页面如下。
<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>
索引.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);
});
请让我知道我做错了什么。我被困在同一个地方有一段时间了。
问候 一月
检查下面的代码,代码中的回调函数返回错误和响应,而不是数据库连接。连接后,您需要使用用于连接的 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);
});
似乎你的错误是说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);
});
希望这些帮助。