我一直在尝试设置一个新的路线“Dashboard”,由于某种原因,每当我尝试访问websiteName / dashboard时我开始使用它的Schema,它会显示一个带有“[]”的空白页面。我不知道发生了什么。
以下是我的路线:
const dashboardController = require('./controllers/dashboard');
app.get('/dashboard', passportConfig.isAuthenticated, dashboardController.getDashboard);
app.post('/dashboard', passportConfig.isAuthenticated, dashboardController.postCreateTodo);
控制器文件夹中的dashboard.js:
const async = require('async');
const crypto = require('crypto');
const nodemailer = require('nodemailer');
const passport = require('passport');
const User = require('../models/User');
const Todo = require('../models/Dashboard');
/**
* GET /dashboard
*
*/
exports.getDashboard = function(req, res){
Todo.find({userId: req.user.id}, function (err, todos) {
if (err) return console.error(err);
res.send(todos);
});
};
/**
* POST /dashboard
*
*/
exports.postCreateTodo = (req, res, next) => {
User.create(req.body.todo, function(err, newTodo){
if(err){
res.render("new");
} else {
//then, redirect to the index
res.redirect("/dashboard");
}
});
};
模型文件夹中的Dashboard.js:
const bcrypt = require('bcrypt-nodejs');
const crypto = require('crypto');
const mongoose = require('mongoose');
const User = require('../models/User');
const 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.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
.frame
.bit-2
h1
| Do
i.fa.fa-plus
input(type='text', 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', 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', 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', 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')
注意:我还没有从CRUD请求的架构中将表单和值添加到dashboard.jade
非常感谢任何帮助,感谢您花时间阅读本文。
答案 0 :(得分:0)
您没有呈现dashboard.jade
视图,您目前只是使用JSON(res.send(todos)
)回复GET请求。
我的猜测是你打算做res.render('dashboard', { todos: todos })
或类似的事情,而不是res.send(todos)
。
此外,在不相关的注释中,当Todo.find()
因错误而失败时,您不会重新编码为GET请求。您至少应该使用res.send(500)
或其他东西来回应客户端内部错误。