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();
  11. var Schema = mongoose.Schema;
  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. });
  24. // on every save, add the date
  25. userSchema.pre('save', function(next) {
  26.   // get the current date
  27.   var currentDate = new Date();
  29.   // change the updated_at field to current date
  30.   this.updated_at = currentDate;
  32.   // if created_at doesn't exist, add to that field
  33.   if (!this.created_at)
  34.     this.created_at = currentDate;
  36.   next();
  37. });
  39. var User = mongoose.model('emedb', userSchema);
  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. });
  57. module.exports = User;
  60. /*
  61. app.use(function(req,res,next){
  62.     req.db = emedb;
  63.     next();
  64. });
  65. */
  67. var routes = require('./routes/index');
  68. var users = require('./routes/users');
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
  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
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)
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
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
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
8,658 Expert Mod 8TB
what file?
Feb 17 '16 #4
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
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
97 64KB
Ok. Will look for that. Thanks for the help.
Feb 17 '16 #7
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

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...
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...
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...
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...
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...
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...
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...
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...
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...
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.......
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
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...
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...
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...
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...
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...
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....
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...
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.