2

I am very new to node JS and mongo.

I am working on a personal website that stores a user's information in my database.

For simplicity, let's say I have the following form in jade...

form(class="inputs", action="/login", method="post")
    input(type="text", name="email",class="form-control", id="emailLogin", placeholder="Queen's Email")

I already set up a database, and I was able to connect to it using the following javascript...

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/user');
var db = mongoose.connection;

db.on('error', console.error);
db.once('open', function() {
    // Create your schemas and models here.
});

I want to store the input from email for every user that registers using the form above.

I am guessing I would first have to create a schema, which would probably look like this, but I'm not sure...

var Schema = mongoose.Schema;

var userSchema = new Schema({
    email: String
});
//I think I have to create a model too?

And to get POST data I think I would need some code that looks like this...

app.post('/login', function(request, response){
//I am not sure what to put inside
});

My question is, can someone show me how to implement all these together so that every time a user registers with their email, it is saved in the database. It is very hard to research this, and have tried and failed many times.

EDIT

Here is my index.js file...

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'QChat' });
});

module.exports = router;

Also, here is another file in my routes directory called users.js, I'm not sure what its purpose is...

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

module.exports = router;
3
  • Where is post() method? Commented Mar 3, 2016 at 7:23
  • @zangw I think I see what you mean, should a post method be included inside the my index file? In that case, I do not have one. Commented Mar 3, 2016 at 7:25
  • To be more precisely, it is better to include post(''/login) to use.js, I guess this file is just for url routing for users? Commented Mar 3, 2016 at 7:27

2 Answers 2

5

Here are some sample codes, hope it could help you.

var userSchema = new Schema({
    email: String
});

var User = mongoose.model('User', userSchema);

app.post('/login', function(request, response){
    var u = new User({
        email: request.body.name
    });

    u.save(function(err) {
        if (err)
           throw err;
        else 
           console.log('save user successfully...');
    });
});

Also to parse the post url correctly, express could be used here, sample codes as below.

var bodyParser = require('body-parser')
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
})); 
Sign up to request clarification or add additional context in comments.

6 Comments

This looks good, but I still cannot get it to work. When I submit the form, nothing gets saved in the db
I'm thinking your code, should work, the reason its not working for me is probably the 404 Not Found that I keep getting when I submit a form
@Bolboa, what is your post url?
Im not exactly sure, here, I'll edit my answer to include my index.js file. Webstorm helped complete much of my code so I am having trouble understanding it all.
I see, I'm not sure where/how to include the post method, but seeing as that is a separate question, I'll still give you best answer. But I would appreciate if you could help resolve the 404 issue
|
1

user.model.js

var mongoose = require('mongoose')
    , Schema = mongoose.Schema
    , Q = require('q')
    ;

var UserSchema = mongoose.Schema({
    email: String,
})

UserSchema.methods.Save = function() {
    return Q.ninvoke(this, 'save');
}

var User = mongoose.model('User', UserSchema);

user.controller.js

var mongoose = require('mongoose')
    , User = mongoose.model('User')
    ;

app.post('/create', function(request, response){
   var user = new User();
   user.email = request.body.email;

    return user.Save().then(function(users) {
        // some code if save succeed
    }, function(err){
        // some code if save failed
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.