Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I have watched and read loads of tutorials now on node.js and mongodb. I know how to create databases, Insert data, show that data on a ejs file, etc etc. However, I have no idea how to insert data from that ejs file.

For example:

I have an app.js and a mongodb.js the app runs my server and the mongodb.js creates a database and inserts some data - not with mongoose but rather with mongodb. I also have a few ejs files that get the data from mongodb.js and displays it. Heres my mongodb.js:

var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/my_database_name';
exports.drinks = [
    { name: 'Manu', rate: 3 },
    { name: 'Martin', rate: 5 },
    { name: 'Bob', rate: 10 }
];
// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
    if (err) {
        console.log('Unable to connect to the mongoDB server. Error:', err);
    } else {
        //HURRAY!! We are connected. :)
        console.log('Connection established to', url);

        // Get the documents collection
        var collection = db.collection('users');

        // Insert some users
        collection.insert(exports.drinks, function (err, result) {
            if (err) {
                console.log(err);
            } else {
                console.log('Inserted %d documents into the "users" collection. The documents inserted with "_id" are:', result.length, result);
            }
    console.log(collection.users.find());
            //Close connection
            db.close();
        });
    }
});

Then in my app.js I have this:

//index page
app.get('/', function(req, res) {
    res.render('pages/index', {
        drinks: drinks,
        tagline: tagline
    });
});

And on my ejs:

<ul>
    <% drinks.forEach(function(drink) { %>
    <li><%= drink.name %> : <%= drink.rate %></li>
    <% });; %>
</ul>

But now I want to be able to update/insert data into my database from my ejs (or even from my app.js).

For example if I have a form and I want the user to type their name inside I want that to be saved in my db. How do I do this?

share|improve this question
    
Your ejs is not saving the data directly to the database but should send the data to your node.js server which takes that data and save it with the mongodb driver api to the database. On a side note: you should consider use mongoose which is a greate abstraction the mongodb for node.js – Shikloshi Sep 14 '15 at 10:34

Ejs only template engine (View in MVC), It cant update and insert database (Controller do this). You need make a route (POST) for get user data and update to database.

share|improve this answer
    
Any tutorials on how to do this? – Manu Sep 14 '15 at 11:26
    
you can read this example stackoverflow.com/questions/26942492/… – trquoccuong Sep 14 '15 at 11:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.