鍍金池/ 問答/數(shù)據(jù)庫  網(wǎng)絡(luò)安全  HTML/ mongoose 如何實(shí)現(xiàn)一個(gè)模塊查詢賦值多個(gè)路由模板?

mongoose 如何實(shí)現(xiàn)一個(gè)模塊查詢賦值多個(gè)路由模板?

如何實(shí)現(xiàn)多個(gè)路由器引用1個(gè)mongodb的模塊(或者骨架)?

簡單代碼例子:
路由:

 router.get('/', async (ctx, next)=> {
        await ctx.render('index',{})
    }
 );
 router.get('/about', async (ctx, next)=>{
        await ctx.render('about',{})
    },
 router.get('/content', async (ctx, next)=>  {
        await ctx.render('content',{})
    },

mongodb查詢一個(gè)集合:

let mongoose = require('mongoose');
let conSchema = mongoose.Schema({

    title:{type:String},
    info:{type:String},
    keywords:{type:String},
    description:{type:String}

});

let conscModel = mongoose.model('cons',conSchema);

把查詢寫在路由上:

let mongoose = require('mongoose');
let conSchema = mongoose.Schema({

    title:{type:String},
    info:{type:String},
    keywords:{type:String},
    description:{type:String}

});

let conscModel = mongoose.model('cons',conSchema);

conscModel .find({},function(err,data) {data});
    
    
    
router.get('/', async (ctx, next)=> {
    await ctx.render('index',{})
});
router.get('/about', async (ctx, next)=>{
    await ctx.render('about',{})
});
router.get('/content', async (ctx, next)=>  {
    await ctx.render('content',{})
});
    

如何實(shí)現(xiàn)都加載?這個(gè)方法1是不行:


let conscModel = mongoose.model('cons',conSchema);
let conn =  conscModel .find({},function(err,data) {data});
    
    
    
router.get('/', async (ctx, next)=> {
      await ctx.render('index',{ web:conn   })
});
router.get('/about', async (ctx, next)=>{
       await ctx.render('about',{ web:conn})
});
 router.get('/content', async (ctx, next)=>  {
       await ctx.render('content',{ web:conn})
});
    

這個(gè)方法2是不行能實(shí)現(xiàn),但是 如果其他查詢是沒辦法寫了:


let conscModel = mongoose.model('cons',conSchema);


router.get('/', async (ctx, next)=> {
     await  conscModel .find({},function(err,data) {
         ctx.render('index',{ web:conn   })
        }
    
     }
});
router.get('/about', async (ctx, next)=>{
         await  conscModel .find({},function(err,data) {
         ctx.render('about',{ web:conn   })
        }
     }
});
router.get('/content', async (ctx, next)=>  {
           await  conscModel .find({},function(err,data) {
         ctx.render('content',{ web:conn   })
        }
     }
});
    

有更好辦法嗎?或者更好的繼承辦法?

回答
編輯回答
久舊酒

分層設(shè)計(jì)
路由和模型之間加一層控制器來處理業(yè)務(wù)邏輯

//以文章為例,文章控制器
'use strict';
import ArticleModel from '../../models/article';
class Article {
    constructor() {
      //...
    }
    async getArticleById(req, res, next) {
        //查找
       let article = await ArticleModel.findOne(...);
    }

    async addArticle(req, res, next) {
       //新增
    }
    // 更新文章需要更新文章評論數(shù)
    // 刪除文章
    async deleteArticle(req, res, next) {
        //刪除
    }
    // 更新文章
    async updateArticle(req, res, next) {
        // 更新
        //這里可以先查找在更新,多次調(diào)用
    }
}
export default new Article();
//文章路由
'use strict';
import express from 'express';
import Article from '../controllers/blog/article';
const router = express.Router();
router.get('/article/:id',  Article.getArticleById);
router.post('/article/add',  Article.addArticle);
router.post('/article/update',  Article.updateArticle);
router.post('/article/delete',  Article.deleteArticle);
export default router

我之前看到的一個(gè)koa+mysql寫的也可以參考下,原理類似
koa-blog

2017年8月24日 20:03