最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - insert post data into mongoDB using Node - Stack Overflow

programmeradmin1浏览0评论

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;

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;
Share Improve this question edited Mar 3, 2016 at 7:19 buydadip asked Mar 3, 2016 at 6:33 buydadipbuydadip 9,43722 gold badges93 silver badges160 bronze badges 3
  • Where is post() method? – zangw 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. – buydadip 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? – zangw Commented Mar 3, 2016 at 7:27
Add a ment  | 

2 Answers 2

Reset to default 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
})); 

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
    });
});
发布评论

评论列表(0)

  1. 暂无评论