To post or create a new job, we need to gather all data from the form in question to create a new ES6 object similar to Python dictionary object before inserting it into MongoDB database by calling insertOne( ) method. In Express.js, all data in the form in question are automatically sent to the request object when the form is submitted, and that data is stored in the ‘body’ object that is a property of the request object.
//route/admin/job.js import express from 'express' const jobRoute = express.Router() import job from '../../controller/admin/job.js' jobRoute.get('/',async (req,res,next)=>{ if(req.session.user){ job.getItem(req,res) }else{ res.redirect('/login') } }) jobRoute.post('/',async (req,res,next)=>{ if(req.session.user){ job.postItem(req,res) }else{ res.redirect('/login') } }) export default jobRoute
// controller/admin/job.js import config from '../../config.js' import categories from '../../model/category.js' import job from '../../model/job.js' class Job{ constructor(){ (async () => { this.config = await config() })() } async getItem(req,res){ this.config.pageTitle = 'ទំព័រការងារ' this.config.route = '/admin/job' this.config.type = 'job' this.config.categories = await categories.getAllItem(req) res.render('base',{data: this.config}) } async postItem(req,res){ await job.insertItem(req) res.redirect('/admin/job') } } export default await new Job()
// model/job.js class Job{ async count(req){ return await req.mydb.collection('jobs').countDocuments() } async insertItem(req){ const id = Date.now() + Math.round(Math.random() * 1E9).toString() let newJob = { id: id, title: req.body.title, content: req.body.content, categories: req.body.categories, payable: req.body.payable, email: req.body.email, website: req.body.website, thumb: req.body.thumb, location: req.body.location, postdate: new Date(), closedate: req.body.closedate, author: req.session.user.id } await req.mydb.collection("jobs").insertOne(newJob) } } export default new Job()
<!--view/admin/job.ejs--> <link rel="stylesheet" href="/styles/admin/job.css" /> <script src="/scripts/ckeditor/ckeditor.js"></script> <script src="/scripts/addCategory.js"></script> <section class="Job"> <form action="/admin/job" method="post"> <input type="text" name="title" required placeholder="ចំណងជើង" /> <textarea name="content" id="editor"></textarea> <input type="text" name="categories" required placeholder="ជំពូក" /> <div class="wrapper"> <select id="category" onChange="getCategory()"> <option>ជ្រើសរើសជំពូក</option> <% for(let v in data.categories){ %> <option><%= data.categories[v].title %></option> <% } %> </select> <input type="text" name="payable" required placeholder="ប្រាក់ឈ្នួល" /> <input type="email" name="email" required placeholder="Email" /> <input type="text" name="website" placeholder="គេហទំព័រ" /> <input type="text" name="thumb" required placeholder="តំណរភ្ជាប់រូបតំណាង"/> <input type="text" name="location" required placeholder="ទីកន្លែង" /> <input type="datetime-local" name="closedate" required /> <input type="submit" value="បញ្ជូន" /> </div> </form> </section> <script src="/scripts/ckeditor/config.js"></script>