我正在尝试使用 Monitoria 集合中的一个字段作为引用进行搜索,我正在使用一个具有自己的路由和方法 GET 的表单,但我没有得到它,我收到此错误并且路由中的这条消息:
Message in the route. "Pesquisar" it's search in portuguese
带有代码的路线:(“Pesquisar”用葡萄牙语搜索)
app.route('monitorias/pesquisar')
.get(function(req, res) {
//var monitoria = req.body.nomeMonitoria;
//console.log(monitoria);
Monitoria.find({name: req.body.nomeMonitoria}, function(err, monitoria) {
if (err) {
res.json(err);
} else {
//res.render('result', {"monitoria": monitoria[0]} );
console.log(monitoria);
}
});
});
html 文件中的表格:(“Pesquisar”是用葡萄牙语搜索的)
<form action="/monitorias/pesquisar" method="get" class="form-inline">
<label class="sr-only" for="nomeMonitoria">Monitoria</label>
<input type="text" class="form-control" id="nomeMonitoria" name="nomeMonitoria" placeholder=" Name of Monitoria...">
<button type="submit" class="btn btn-primary">Search</button>
</form>
和 Monitoria 的模式:
var MonitoriaSchema = new Schema({
name: {
type: String,
required: 'Por favor, entre com o nome da monitoria'
},
code: {
type: String,
required: 'Por favor, entre com o código da monitoria',
},
oferta: {
type: String,
default: '2017-02'
},
local: {
type: String,
default: null
},
googlemaps: {
type: String,
default: null
},
Horarios: {
type: [String],
default: null
}
});
我已经被困在里面好几个小时了。感谢您的帮助。
最佳答案
已解决!
错误的路线:
app.route('monitorias/pesquisar')
-> app.route('/monitorias/pesquisar')
req 参数也是错误的,在函数中将 req.body.nomeMonitoria
更改为 req.query.nomeMonitoria
所以:
app.route('monitorias/pesquisar')
.get(function(req, res) {
Monitoria.find({name: req.query.nomeMonitoria}, function(err, monitoria) {
if (err) {
res.json(err);
} else {
res.render('result', {"monitoria": monitoria} );
}
});
});
关于javascript - 使用 mongoose、express 和 node.js 搜索表单。错误 : can't set headers after they are sent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46877972/