如何从Express Server和Node.js筛选GET查询

时间:2018-11-24 19:58:13

标签: node.js mongodb express mongoose mean-stack

我想创建一个GET路由,该路由可以获取我的所有任务,但仅包含具有task_completed属性boolean'false'的任务。

现有路线:

router.get('/getalltasks', cors(), async(req, res) => {
Task.find(function(err, tasks) {

// if there is an error retrieving, send the error. 
                // nothing after res.send(err) will execute
if (err)
    res.send(err);


res.json(tasks); // return all tasks that are in JSON format 

  });
});

MongoosSchema:

const mongoose = require('mongoose');

const TaskSchema = new mongoose.Schema({
task_name:{
    type: String,
    required: true,
    minlength: 1,
    unique: true,
},
task_category: String,
task_xpreward: Number,
task_completed: Boolean,
task_difficulty: Number,    //1 = Easy, 2 = Medium, 3 = Hard, 4 = Very Hard, 5 = Impossible
task_city : String,
});

 //sommige variabelen kunnen opgedeeld worden in 2de schema met relatie 
module.exports = mongoose.model('Task', TaskSchema);

如何在现有代码中实现呢?

1 个答案:

答案 0 :(得分:2)

因此,如果您想要一项具有>>> U[2:] = []; >>> U [2, [5, 6, 9]] 属性的任务:

编辑:捕获错误:

task_completed: false

,如果要使用router.get('/getalltasks', cors(), async(req, res) => { let task; try{ task = await Task.findOne({task_completed: false}); }catche (e){ console.log(`Err: ${e}`); } res.json(task); } 执行所有任务:

task_completed: false