node.js - 如何使提交的表单在出现在索引页上之前经过批准

标签 node.js mongodb mongoose admin ejs

您好,我正在构建一个应用程序,以便人们能够在求职板上发布职位。但目前,如果发布了职位,它会直接进入主页/索引页面,我不希望这样。我希望能够在职位发布在职位公告板/主页/索引页面上发布之前对其进行查看。有这方面的教程吗?

我目前正在使用之前的 Node 教程来构建这个项目。

// Example code, but similar to actual code

//models config
let jobSchema = new mongoose.Schema({
    title: String,
    category: String,
    description: String,
    type: String,
    url: String,
    email: String,
    apply: String,
    location: String,
    company: String,
    path: String,
    created: {type: Date, default: Date.now}
})

let Job = mongoose.model('job', jobSchema);
// {type: String, default: "placeholdeimage.jpg"}

//routes config

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './public/uploads/');
    },

    filename: function (req, file, cb) {
        var originalname = file.originalname;
        var extension = originalname.split(".");
        filename = Date.now() + '.' + extension[extension.length-1];
        cb(null, filename);
    }
});


app.get('/', (req, res) =>{
    res.redirect('/jobs')
})

app.get('/jobs',  (req, res) =>{
    Job.find({}).sort({ date: -1}).find(function(err, jobs){
        if(err){
            console.log('error')
        }

        return res.render('index', {jobs: jobs})

    })

})

//add form route
app.get('/jobs/add', (req, res) => {
    res.render('add')
})

app.post('/jobs', multer({storage: storage, dest: 
'./public/uploads/'}).single('file'), (req, res) => {

    req.body.description = req.sanitize(req.body.description);

    Job.create( (err, addJob) =>{

        if(req.file){
            let fullPath = "uploads/"+req.file.filename;
            let document = {
                title: req.body.title,
                category: req.body.category,
                description: req.body.description,
                type: req.body.type,
                url: req.body.url,
                email: req.body.email,
                apply: req.body.apply,
                location: req.body.location,
                company: req.body.company,
                path: fullPath
            };

            let job = new Job(document); 
                job.save()
        }else{
            console.log('file not uploaded')
            logo = 'noimage.jpg'
        }

        //redirect to index page
        return res.redirect('/jobs')
    })
})

app.get('/jobs/:id', (req, res) => {
    Job.findById(req.params.id, (err, jobDetails) => {
        if(err){
            res.redirect('/jobs')
        }else{
            res.render('details', {job: jobDetails});
        }
    })
})


app.listen(port, process.env.PORT, process.env.IP, ()=> console.log(`Server 
is running on ${port}`))
<div class="container">

  <% for (const job of jobs) { %>
    <div class="row">
      <div class="column logo" style="width:10%;">
        <img src='<%= job.path %>'>
      </div>
      <div class="column title">
        <h2>
          <%=job.title%>
        </h2>
        <p>
          <%=job.company%>
        </p>
      </div>
      <div class="column type">
        <h2>
          <%=job.type%>
        </h2>
      </div>
      <div class="column location">
        <h2>
          <%=job.location%>
        </h2>
      </div>
      <div class="column">
        <h2><a href="/jobs/<%= job._id %>"><button>Apply</button></a></h2>
      </div>
    </div>
    <hr>
  <% } %>

</div>

最佳答案

您可以向 JobSchema 添加字段 approved: { type: Boolean, default: false } 并根据条件检索职位。

Job.find({approved:true}).sort({ date: -1})...

关于node.js - 如何使提交的表单在出现在索引页上之前经过批准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52356760/

相关文章:

node.js - NodeJS 共享对象,无需多次实例化

node.js - 在 Express.js 应用上上传文件

使用 MongoDB 驱动程序时出现 java.lang.NoClassDefFoundError

javascript - Mongoose - RangeError : Maximum Call Stack Size Exceeded

angularjs - 发送后无法设置 header - 通过 NodeJs 发布新用户

javascript - NodeJS + MongoDB - 启动从 npm 安装的 mongodb 服务器实例

node.js - 余额转账示例的invoke-transaction.js中的交易ID响应有什么用?

node.js - Selenium-webdriver 无法在 NodeJS 中从 Mocha 打开 Firefox

node.js - Nodejs + Mongodb 登录示例不起作用

java - 将 MongoDb 查询转换为 Java BasicDbObject