I am currently building an app using MEAN stack. For which i have a form and need to insert the form data into the mongoDB database.
But i get the error "(node:1888) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: invalid schema, expected mongodb connected to db NaN".
and also TypeError: Cannot read property 'insert' of undefined at C:\Users\Avvijan\meanexamples\appointment\routes\main.js:64:12
please help!
My code snippet is
<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>
<button type="submit" class="btn btn-primary" id="save">Submit</button>
</form>
and my routes->main.js file is
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
const config = require('../config/database');
var mongo = require('mongodb');
var dburl = "mongodb://localhost:27017/appointment";
var db = mongo('appointment',['appoint']);
//connect to db
mongoose.connect(config.database);
//check connection
mongoose.connection.on("connected",function(){
console.log('connected to db',+config.database);
});
mongoose.connection.on("error",function(err){
console.log("error",+err);
});
router.post('/process',function(req,res){
var newClient = {
firstName : req.body.fName,
lastName : req.body.lName,
email : req.body.email,
phone : req.body.phone,
dob : req.body.dob,
gender : req.body.gender,
};
db.appoint.insert(newClient,function(err,result){
if(err) { throw err; }
else{
res.redirect('/thanks');
}
});
console.log("firstName:" +req.body.fName);
console.log("lastName:"+req.body.lName);
console.log('email:'+req.body.email);
});
Please advice what is the mistake im making here. :( Thanks in advance. Regards, Janani
via janani
No comments:
Post a Comment