By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,655 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,655 IT Pros & Developers. It's quick & easy.

Error Message: Cannot read property of undefined - MongoDb, nodejs

P: 97
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:

Expand|Select|Wrap|Line Numbers
  1. var express = require('express');
  2. var path = require('path');
  3. var favicon = require('serve-favicon');
  4. var logger = require('morgan');
  5. var cookieParser = require('cookie-parser');
  6. var bodyParser = require('body-parser');
  7. var mongoose = require('mongoose');
  8. mongoose.connect('mongodb://localhost/emedb');
  9. var app = express();
  10.  
  11. var Schema = mongoose.Schema;
  12.  
  13. // create a schema
  14. var userSchema = new Schema({
  15.   moniker: { type: String, required: true,},
  16.   email: { type: String, required: true,},
  17.   password: { type: String, required: true },
  18.   location: String,
  19.   age: Number,
  20.   created_at: Date,
  21.   updated_at: Date
  22. });
  23.  
  24. // on every save, add the date
  25. userSchema.pre('save', function(next) {
  26.   // get the current date
  27.   var currentDate = new Date();
  28.  
  29.   // change the updated_at field to current date
  30.   this.updated_at = currentDate;
  31.  
  32.   // if created_at doesn't exist, add to that field
  33.   if (!this.created_at)
  34.     this.created_at = currentDate;
  35.  
  36.   next();
  37. });
  38.  
  39. var User = mongoose.model('emedb', userSchema);
  40.  
  41. // post to db
  42. app.post('/newBen', function(req, res){
  43.     new User({
  44.         moniker: req.body.moniker,
  45.         email: req.body.email, 
  46.         password: req.body.password,
  47.         location: req.body.location,
  48.         age: req.body.age,
  49.         created_at: req.currentDate,
  50.         updated_at: req.currentDate
  51.     }).save(function(err, doc){
  52.         if(err)res.json(err);
  53.         else res.send('Thank you for joining');
  54.     });res.end();
  55. });
  56.  
  57. module.exports = User;
  58.  
  59.  
  60. /*
  61. app.use(function(req,res,next){
  62.     req.db = emedb;
  63.     next();
  64. });
  65. */
  66.  
  67. var routes = require('./routes/index');
  68. var users = require('./routes/users');
  69.  
  70.  
And this is my jade file with the form that the data is supposed to be sent from:

Expand|Select|Wrap|Line Numbers
  1. extends layout
  2.  
  3. block content
  4.   form(name='regBenForm' class='regForm' action='/newBen' method='post')
  5.     label(for='moniker') Name
  6.     input(type="text", name="moniker", id="moniker")
  7.     label(for='email') Email
  8.     input(type="email", name="email")
  9.     label(for='password') Password
  10.     input(type="password", name="password")
  11.     label(for='passwordMatch') Repeat Password
  12.     input(type="password", name="passwordMatch")
  13.     label(for='age') Age
  14.     input(type="number", name="age")
  15.     label(for='location') Location
  16.     input(type="text", name="location")
  17.     button(type="submit" class='submitBtn') Go
  18.  
And this is the error message I get in the broswer:

Expand|Select|Wrap|Line Numbers
  1. TypeError: Cannot read property 'moniker' of undefined
  2.     at D:\node\eme\app.js:47:20
  3.     at Layer.handle [as handle_request] (D:\node\eme\node_modules\express\lib\router\layer.js:95:5)
  4.     at next (D:\node\eme\node_modules\express\lib\router\route.js:131:13)
  5.     at Route.dispatch (D:\node\eme\node_modules\express\lib\router\route.js:112:3)
  6.     at Layer.handle [as handle_request] (D:\node\eme\node_modules\express\lib\router\layer.js:95:5)
  7.     at D:\node\eme\node_modules\express\lib\router\index.js:277:22
  8.     at Function.process_params (D:\node\eme\node_modules\express\lib\router\index.js:330:12)
  9.     at next (D:\node\eme\node_modules\express\lib\router\index.js:271:10)
  10.     at expressInit (D:\node\eme\node_modules\express\lib\middleware\init.js:33:5)
  11.     at Layer.handle [as handle_request] (D:\node\eme\node_modules\express\lib\router\layer.js:95:5)
  12.     at trim_prefix (D:\node\eme\node_modules\express\lib\router\index.js:312:13)
  13.     at D:\node\eme\node_modules\express\lib\router\index.js:280:7
  14.     at Function.process_params (D:\node\eme\node_modules\express\lib\router\index.js:330:12)
  15.     at next (D:\node\eme\node_modules\express\lib\router\index.js:271:10)
  16.     at query (D:\node\eme\node_modules\express\lib\middleware\query.js:49:5)
  17.     at Layer.handle [as handle_request] (D:\node\eme\node_modules\express\lib\router\layer.js:95:5)
  18.  
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.
Feb 12 '16 #1
Share this Question
Share on Google+
6 Replies


Dormilich
Expert Mod 5K+
P: 8,639
from the message it looks req.body does not exist. I donít know which version of express youíre using, so I can only point out its documentation.
Feb 12 '16 #2

P: 97
That seems correct. I removed .body from all of the items to be posted to the db. Now, when I run a test in the browser, it redirects to the action of the form, which is /newBen. Another quick question. Do I use that file to post the data to the db, or can I use it to redirect to a new page, or both?
Feb 17 '16 #3

Dormilich
Expert Mod 5K+
P: 8,639
what file?
Feb 17 '16 #4

P: 97
The /newBen file. I could use that a redirect. But it looks like the data I sent from the browser wasn't inserted to the db. I don't suppose you know of any MongoDB tutorials that would explain it?
Feb 17 '16 #5

Dormilich
Expert Mod 5K+
P: 8,639
The /newBen file.
there is no such file. /newBen is a route in your application that is handled by the function defined in Express.

I don't suppose you know of any MongoDB tutorials that would explain it?
I think you would need an Express tutorial, rather than a mongoose tutorial (a MongoDB tutorial won’t help as you don’t interact directly with MongoDB as would be the case if you’d use the MongoDB client on the command line)
Feb 17 '16 #6

P: 97
Ok. Will look for that. Thanks for the help.
Feb 17 '16 #7

Post your reply

Sign in to post your reply or Sign up for a free account.