Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to the MEAN stack. Could someone please help me understand this:
I create a new task and save it, but in the index page I can only see.
_id: 5834ba711401ea28643cdfb8, __v: 0
No other attributes. However docs.length in tasks/index.jade grows with every added task.
What am I doing wrong?
Thank you very much!
tasks.js
JavaScript
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/todo_development');

var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
// #9
var Task = new Schema({
    task: String // #11
});
var Task = mongoose.model('Task', Task); // #10

router.get('/', function(req, res) {
    Task.find({}, function (err, docs){
        res.render('tasks/index', { 
            title: ' Ornbo | Todos index view' ,
            docs: docs // #12 
        });
    });
});

// @4
router.get('/new', function(req,res){
    res.render('tasks/new.jade', {
        title: 'New Task'
    });
});

//#5
router.post('/', function(req, res){
    var task = new Task(req.body.item); // #6
    task.save(function(err){ // #7
        if(!err){
            res.redirect('/tasks'); // #8
        } else {
            res.redirect('/tasks/new');
        }
    });

});

module.exports = router;


tasks/index.jade
JavaScript
- if(docs.length)
        span number of records: #{docs.length}
        table
            tr
                th Task
                    each task in docs
                        tr
                            td Do: [#{task}]


What I have tried:

I have tried renaming the attribute and looking at the DB in MongoVUE.
Posted
Updated 24-Nov-16 7:41am

1 solution

This code fixed the situation:
JavaScript
app.use(bodyParser.urlencoded({ extended: true }));

req.body.task was undefined until extended was set to true to support URL-encoded bodies.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900