I'm trying to add a MongoDb database to a nodejs app. I've never done this before and don't know how it works. I seem to have the Db installed, but I don't seem to be able to send data to it from a web form.
I have this in my app.js file:
And this is my jade file with the form that the data is supposed to be sent from:
And this is the error message I get in the broswer:
I'm not sure if it's to do with the code I've commented out in the app.js file - around line 60 - or I just haven't properly called the form input.
I have this in my app.js file:
Code:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/emedb');
var app = express();
var Schema = mongoose.Schema;
// create a schema
var userSchema = new Schema({
moniker: { type: String, required: true,},
email: { type: String, required: true,},
password: { type: String, required: true },
location: String,
age: Number,
created_at: Date,
updated_at: Date
});
// on every save, add the date
userSchema.pre('save', function(next) {
// get the current date
var currentDate = new Date();
// change the updated_at field to current date
this.updated_at = currentDate;
// if created_at doesn't exist, add to that field
if (!this.created_at)
this.created_at = currentDate;
next();
});
var User = mongoose.model('emedb', userSchema);
// post to db
app.post('/newBen', function(req, res){
new User({
moniker: req.body.moniker,
email: req.body.email,
password: req.body.password,
location: req.body.location,
age: req.body.age,
created_at: req.currentDate,
updated_at: req.currentDate
}).save(function(err, doc){
if(err)res.json(err);
else res.send('Thank you for joining');
});res.end();
});
module.exports = User;
/*
app.use(function(req,res,next){
req.db = emedb;
next();
});
*/
var routes = require('./routes/index');
var users = require('./routes/users');
Code:
extends layout
block content
form(name='regBenForm' class='regForm' action='/newBen' method='post')
label(for='moniker') Name
input(type="text", name="moniker", id="moniker")
label(for='email') Email
input(type="email", name="email")
label(for='password') Password
input(type="password", name="password")
label(for='passwordMatch') Repeat Password
input(type="password", name="passwordMatch")
label(for='age') Age
input(type="number", name="age")
label(for='location') Location
input(type="text", name="location")
button(type="submit" class='submitBtn') Go
Code:
TypeError: Cannot read property 'moniker' of undefined
at D:\node\eme\app.js:47:20
at Layer.handle [as handle_request] (D:\node\eme\node_modules\express\lib\router\layer.js:95:5)
at next (D:\node\eme\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (D:\node\eme\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\node\eme\node_modules\express\lib\router\layer.js:95:5)
at D:\node\eme\node_modules\express\lib\router\index.js:277:22
at Function.process_params (D:\node\eme\node_modules\express\lib\router\index.js:330:12)
at next (D:\node\eme\node_modules\express\lib\router\index.js:271:10)
at expressInit (D:\node\eme\node_modules\express\lib\middleware\init.js:33:5)
at Layer.handle [as handle_request] (D:\node\eme\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\node\eme\node_modules\express\lib\router\index.js:312:13)
at D:\node\eme\node_modules\express\lib\router\index.js:280:7
at Function.process_params (D:\node\eme\node_modules\express\lib\router\index.js:330:12)
at next (D:\node\eme\node_modules\express\lib\router\index.js:271:10)
at query (D:\node\eme\node_modules\express\lib\middleware\query.js:49:5)
at Layer.handle [as handle_request] (D:\node\eme\node_modules\express\lib\router\layer.js:95:5)
Comment