nodejs / mongoDB - TypeError:无法读取未定义

时间:2017-06-01 22:13:26

标签: javascript node.js mongodb mean-stack

我正在尝试将表单数据提交到mongoDB数据库中。但我无法做到这一点。在我的index.js文件中获取“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);
});

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

此致 扬

2 个答案:

答案 0 :(得分:1)

检查下面的代码,代码中的回调函数返回错误和响应,而不是数据库连接。连接后,您需要使用用于连接的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);
});

答案 1 :(得分:0)

您的错误似乎是db未定义,因此当它试图从.collection获取db.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);
});

希望那些帮助。