2

Heres my problem. Might be a bit trivial. I am using node.js to write a form which has radio buttons, drop down boxes. I have been able to save data and also retrieve it successfully but I am not able to write it onto web page. What is the correct way to write the data onto a page

1
  • 2
    Welcome to StackOverflow Aston. Please include some of your code, so that members of the community can help you. To help you without any code, could be very complex and time consuming. Commented Jun 23, 2013 at 1:02

1 Answer 1

6

You can do this pretty easily with express and mongoose. First you would connect to mongoDB using mongoose, and then set up some of the variables used to interact with mongoDB from mongoose (i.e. mongoose.scheme & mongoose.model), and finally you simply send your mongoDB data to a web page through express's res.render function:

mongoose.connect('mongodb://localhost/test', function(err){
    if(!err){
        console.log('connected to mongoDB');
    } else{
        throw err;
    }
});

var Schema = mongoose.Schema,
    ObjectID = Schema.ObjectID;

var Person = new Schema({
    name : String
});

var Person = mongoose.model('Person', Person);   

app.get('/', function(req, res){
    Person.find({}, function(err, docs){
        res.render('index', { docs: docs});
    });
});

After sending the data, you can simply reference the 'docs' variable in your web page. Express automatically uses the Jade framework. In Jade you could do something like list all the names of the people in your database:

- if(docs.length)
    each person in docs
      p #{person.name}
- else
    p No one is in your database!
Sign up to request clarification or add additional context in comments.

4 Comments

No problem! If your question is solved, please accept the answer (click the check mark underneath the down arrow).
Are you sure you referenced the right jade file in res.render('index', { docs: docs}); ? I.e. are you actually using docs in your index.jade file?
as i am doing it in index.jade and referencing the same!
Can you put console.log(docs) before res.render('index', { docs: docs}); and say what it outputs?

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.