469,142 Members | 1,172 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,142 developers. It's quick & easy.

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

97 64KB
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
7 6922
Dormilich
8,651 Expert Mod 8TB
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
tdrsam
97 64KB
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
8,651 Expert Mod 8TB
what file?
Feb 17 '16 #4
tdrsam
97 64KB
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
8,651 Expert Mod 8TB
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
tdrsam
97 64KB
Ok. Will look for that. Thanks for the help.
Feb 17 '16 #7
mqasimkhan
1 Bit
{"errors":{"email":{"name":"ValidatorError","messa ge":"Cannot read property 'isEmail' of undefined","properties":{"message":"Cannot read property 'isEmail' of undefined","type":"user defined","path":"email","value":"qasim.softeng29@g mail.com","reason":{}},"kind":"user defined","path":"email","value":"qasim.softeng29@g mail.com","reason":{}},"address":{"name":"Validato rError","message":"Path `address` (`Bannu`) is shorter than the minimum allowed length (10).","properties":{"message":"Path `address` (`Bannu`) is shorter than the minimum allowed length (10).","type":"minlength","minlength":10,"path":"a ddress","value":"Bannu"},"kind":"minlength","path" :"address","value":"Bannu"},"address2":{"name":"Va lidatorError","message":"Path `address2` (`New York`) is shorter than the minimum allowed length (10).","properties":{"message":"Path `address2` (`New York`) is shorter than the minimum allowed length (10).","type":"minlength","minlength":10,"path":"a ddress2","value":"New York"},"kind":"minlength","path":"address2","value ":"New York"}},"_message":"User validation failed","name":"ValidationError","message":"User validation failed: email: Cannot read property 'isEmail' of undefined, address: Path `address` (`Bannu`) is shorter than the minimum allowed length (10)., address2: Path `address2` (`New York`) is shorter than the minimum allowed length (10)."}
2 Weeks Ago #8

Post your reply

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

Similar topics

1 post views Thread by Hal Halloway | last post: by
2 posts views Thread by Patrick Reany | last post: by
7 posts views Thread by Tuvas | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.