how do I implement a module (or skeleton) in which multiple routers reference one mongodb?
simple code example:
routing:
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 query a collection:
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);
write the query on the route:
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",{})
});
how to load all? This method 1 is not good:
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})
});
this method 2 cannot be implemented, but if other queries cannot be written:
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 })
}
}
});
is there a better way? Or a better way to inherit?