472,353 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 8403
Dormilich
8,658 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,658 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,658 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)."}
Sep 11 '21 #8

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

Similar topics

1
by: Hal Halloway | last post by:
How do I fix this error message below? Thanks. Notice: Undefined index: key_word in C:\Program Files\Apache Group\Apache2\htdocs\search010.php...
1
by: B Golash | last post by:
Hi I`m trying to install a third-party control. During the install I receive this error...see the url below. I have no idea as to the meaning of...
2
by: Patrick Reany | last post by:
I wrote a C++ progam nine years ago and I haven't been in C++ since then. I re-installed Visual C++ 4.0 on a Win98 machine and tried to compile the...
7
by: Tuvas | last post by:
Okay, so I've been getting this error message when trying to use PIL to open a JPEG, that there isn't a library by the name of libtiff.so.3 . I've...
0
by: tom | last post by:
When I try to read in a csv file it gives me this error message. 'Cannot update. Database or object is read-only.' If I change the extension to...
1
by: DotNetNewbie | last post by:
Hello, I have a validation summary tag in my .aspx page, and when a user clicks on a submit button I check the database for a duplicate username...
1
by: mrkarthic | last post by:
We are using SQL Server 2005 Express Edition. It has been observed that sometimes our application crashes and from the logs recovered the following...
3
by: Yousef Altaf | last post by:
hay I have this code to upload images it works OK bu there is 2 things not working 1- when the image has the same name it doesn't display the...
7
by: belz | last post by:
i have the following function for calculating distances.this function is the distance call back function.it goes throught all the alert statements...
1
by: chinmayjape244 | last post by:
When i compile a program an error message occur that: cannot convert 'unsigned long' to 'char far*' . Please suggest how to handle this.......
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.