I am trying to submit the form data into mongoDB database. But i have trouble doing it. Getting "TypeError: Cannot read property 'collection' of undefined" error in my index.js file My form page is as follows.
<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>
the index.js file is
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);
});
Please let me know what im doing wrong. I am stuck in the same place for a while.
Regards, Jan
via janani
No comments:
Post a Comment