无法将Todos保存给用户

时间:2016-08-07 23:30:37

标签: javascript node.js schema

我已经在网上搜索了几天,我似乎无法找到任何可能对我的情况有所帮助的东西。我正在尝试将用户的输入(来自dashboard.jade中的表单)保存为模式中的待办事项,但由于某种原因它无法保存。我希望有人可以帮助我。对不起,我知道下面有很多代码。感谢您阅读本文!

app.js中的路线:

app.get('/dashboard', passportConfig.isAuthenticated, dashboardController.getDashboard);
app.post('/dashboard', passportConfig.isAuthenticated, dashboardController.postCreateTodo);

dashboard.js中的架构:

const mongoose = require('mongoose');

var todoSchema = new mongoose.Schema({
  name: {type: String, default : ''},
  user: {type: mongoose.Schema.ObjectId, ref: 'user'},
  createdAt  : {type : Date, default : Date.now}
});
const Todo = mongoose.model('Todo', todoSchema);

module.exports = Todo;

dashboard.js中的控制器:

const Todo = require('../models/Dashboard');

/**
 * GET /dashboard
 * 
 */
exports.getDashboard = function(req, res){
  res.render('dashboard', { 
    user: req.user,
    title: 'Dashboard',
  });
};

/**
* POST /dashboard
* 
*/
exports.postCreateTodo = (req, res, next) => {
  console.log(req.body.todo); 
  var todo = new Todo(req.body.todo);
  todo.user = req.user._id;
  todo.save(function (err) {
     if (!err) {
      res.redirect('/dashboard');
     }
     else {
      return next(err);
     }
  });
};

jade in dashboard.jade:

extends layout

block additionalCSS
  link(rel='stylesheet', type='text/css', href='assets/css/todos.css')
  link(href='https://fonts.googleapis.com/css?family=Roboto:400,700,500', rel='stylesheet', type='text/css')
  link(rel='stylesheet', type='text/css', href=' https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css')
  link(rel='stylesheet', href='//cdnjs.cloudflare.com/ajax/libs/lemonade/2.1.0/lemonade.min.css')
  script(type='text/javascript', src='assets/plugins/jquery-3.0.0.min.js')


block content
  #container
    .page-header
      h3 Dashboard
    form(method='post', action='/dashboard')
      fieldset
        .frame
          .bit-2
            h1
              | Do 
              i.fa.fa-plus             
            input(type='text', name='todo[name]', placeholder='Add New Todo') 
            ul
              li
                span
                  i.fa.fa-trash
                |  Finish Daily Report              
              li
                span
                  i.fa.fa-trash
                |  House on Fire              
              li
                span
                  i.fa.fa-trash
                |  Crying Baby
          .bit-2
            h1
              | Decide 
              i.fa.fa-plus           
            input(type='text', name='todo[name]', placeholder='Add New Todo')            
            ul
              li
                span
                  i.fa.fa-trash
                |  Exercising               
              li
                span
                  i.fa.fa-trash
                |  Calling Family And Friends              
              li
                span
                  i.fa.fa-trash
                |  Long-term Biz Strategy
        .frame
         .bit-2
            h1
              | Delegate 
              i.fa.fa-plus             
            input(type='text', name='todo[name]', placeholder='Add New Todo')            
            ul
              li
                span
                  i.fa.fa-trash
                |  Scheduling Interviews              
              li
                span
                  i.fa.fa-trash
                |  Booking Flights             
              li
                span
                  i.fa.fa-trash
                |  Answering Certain emails         
          .bit-2
            h1
              | Delete 
              i.fa.fa-plus            
            input(type='text', name='todo[name]', placeholder='Add New Todo')            
            ul
              li
                span
                  i.fa.fa-trash
                |  Watching Television              
              li
                span
                  i.fa.fa-trash
                |  Checking Social Media              
              li
                span
                  i.fa.fa-trash
                |  Surfing The Web

block additionalJS  
  script(type='text/javascript', src='assets/js/todos.js')

这是todos.js:

/* global $ */

//Check off Specific Todos by Clicking
$("ul").on("click", "li", function(){
    $(this).toggleClass("completed");
});

//Click on x to delete todo
$("ul").on("click", "span", function(event){
    $(this).parent().fadeOut(500, function(){
        $(this).remove();
    });
    event.stopPropagation();
});

$(document).ready(function(){
  $("input[type = 'text']").keypress(function(event){

    if(event.which === 13){
      //grabbing new todo from input
      var todoText = $(this).val();
      $(this).val("");
      //create a new li and add to ul
      $(this).next("ul").append("<li><span><i class='fa fa-trash'></i></span> " + todoText + "</li>");
      event.preventDefault();
      $("form").submit();
    }
  });
});

$(".fa-plus").click(function(){
    $("input[type='text'").fadeToggle();
});

0 个答案:

没有答案