我正在使用Express和Mongoose构建一个经典的Todo服务器。这是我的模型:
import mongoose = require('mongoose');
const autoIncrement = require('mongoose-sequence')(mongoose);
const TodoSchema: mongoose.Schema = new mongoose.Schema({
todoid: {
type: Number
},
title: {
type: String,
required: 'Enter a title'
},
note: {
type: String
},
complete: {
type: Boolean,
default: false
},
editMode: {
type: Boolean,
default: false
}
});
TodoSchema.plugin(autoIncrement, {
inc_field: 'todoid',
start_seq: 422
});
export { TodoSchema };我想要处理以下REST API查询:
http://localhost:3000/todos?complete=true
我可以执行FindOne之类的基本操作,但我似乎找不出代码来过滤GET调用的结果,从而只返回完成的待办事项。
那么正确的方法是什么呢?
发布于 2019-09-20 14:27:49
您可以使用find函数根据completed进行查询:
async function getTodohandler(req, res){
var result = await TodoSchema.find({completed: req.query.completed == "true"})
return res.send(result)
}https://stackoverflow.com/questions/58020818
复制相似问题