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: -
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');
-
-
And this is my jade file with the form that the data is supposed to be sent from: -
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
-
And this is the error message I get in the broswer: -
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)
-
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.
7 8403
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.
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?
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?
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)
Ok. Will look for that. Thanks for the help.
{"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)."}
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...
| | |