[英]Search form using mongoose, express and node.js. Error: can't set headers after they are sent
我正在嘗試使用Monitoria集合中的字段作為參考進行搜索,正在使用具有自己的路線和GET方法的表單,但我沒有得到它,出現此錯誤,並且在路線:
帶有代碼的路線:(“ 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} );
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.