First, create a table structure /schemas/categories.js
var mongoose = require('mongoose')
module.exports = new mongoose.Schema({
category_name : String,
})
Second, create a model /models/Categories.js
var mongoose = require('mongoose')
var categoriesSchema = require('../schemas/categories')
module.exports= mongoose.model('Categories',categoriesSchema)
Third, the front desk (use template syntax):
(A), Home blog Category: /views/category.html
{% extends 'layout.html' %}
{% block main %}
<ol class="breadcrumb">
<li><a href="/admin">管理首页</a></li>
<Li> <span> Category List </ span> </ li>
</ol>
Category List <h3> </ h3>
<table class="table table-bordered">
<tr>
<Th> category name </ th>
</tr>
<! -. Id is an object cycle of the output array, to be converted into a string ->
{% for category in categories %}
<tr>
<td> {{category.category_name}}</td>
{% endfor %}
</tr>
</table>
{% endblock %}
(B), add blog category page: views / category_add.html
{% extends 'layout.html' %}
{% block main %}
<ol class="breadcrumb">
<li><a href="/admin">管理首页</a></li>
<Li> <span> Add categories </ span> </ li>
</ol>
<H3> Add categories </ h3>
<form method="POST">
<div class="form-group">
<Label for = "category_name"> category name </ label>
<input type="text" class="form-control" name="category_name" id="category_name" placeholder="Category Name">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
{% endblock %}
Fourth, back into the model /router/admin.js
var Category = require('../models/Category')
**
* Category Management
*/
router.get ( '/ category', (req, res) => {// a /, instead ./
Category.find().then((categories)=>{
res.render('admin/category',{
userInfo: req.userInfo,
categories:categories
})
})
})
/**
* add category
*/
router.get('/category/add',(req,res)=>{
res.render('admin/category_add',{
userInfo:req.userInfo
})
})
/**
* Save classifications, verify the information received form submission
*/
router.post('/category/add',(req,res)=>{
// console.log(req.body)
// When the user submits the wrong message, the wrong page is returned (because the reception is not set AJAX) => render an error page (generic error page: admin / error)
var category_name = req.body.category_name || ''
if(category_name == ''){
res.render('admin/error',{
userInfo:req.userInfo,
message: 'category name can not be empty'
url:'/admin/category/add'
})
return // interrupt
}
// query whether there is a category name just upload databases, information classification name that already exists is returned, there is no category name is uploaded memory
Category.findOne({
category_name:category_name
}).then((fs)=>{
/**
* [Bug] here can not fill category_name.
* Argument can not be the object query (empty), the results should be returned by polling
* Then method returns a Promise objects
* Final state of asynchronous operations ( "completion or failure"), and the result value of the asynchronous operation.
* */
if(fs){
res.render('admin/error',{
userInfo:req.userInfo,
message: 'Category name already exists'
url:'/admin/category/add'
})
return Promise.reject () // Returns an object with a rejection reason Promise reason parameter. It represents state failure
}else{
// save
return new Category({
category_name:category_name
}).save()
}
}).then((newCategory)=>{
if(newCategory){
res.render('admin/success',{
userInfo:req.userInfo,
message: 'Add categories success'
url:'/admin/category'
})
}
})