我有以下HTML表单
<form id="contact-form" method="POST" action="/search">
<label for="company">Phone company</label>
<input type="text" name="company" value="">
<br>
<label for="modelname">Model name</label>
<input type="text" name="modelname" value="">
<br>
<label for="numbername">Model number</label>
<input type="text" name="numbername" value="">
<br>
<input type="submit" value="Search">
</form>
手机架构
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const phoneSchema = new Schema({
company: String,
modelname: String,
numbername: String,
picture: String,
price: String,
});
const Phone = mongoose.model('phone', phoneSchema);
module.exports = Phone;
例如,我的数据库中有下一部手机:
{
"_id" : ObjectId("5b155a66aced9b079c276ba0"),
"company" : "Samsung",
"modelname" : "Galaxy",
"numbername" : "S5",
"picture" : "https://drop.ndtv.com/TECH/product_database/images/2252014124325AM_635_samsung_galaxy_s5.jpeg",
"price" : "12500P",
"__v" : 0
}
发布请求处理程序
app.post('/search', urlencodedParser, function(req, res){
console.log(req.body);
Phone.findOne({company: req.body.company, modelname: req.body.modelname, numbername: req.body.numbername}).then(function(){
.......
})});
如果此表单中的company,modelname,numbername与相同的数据库字段相同,那么我必须使用数据库中的所有信息(例如公司+图片+价格)呈现页面。如何比较表单数据和数据库数据?< / p>
答案 0 :(得分:0)
我如何理解您正在尝试将数据库信息传递回HTML页面。我认为这是一个类似的问题:How do I serve partially dynamic HTML pages with Express? 如果您需要过滤任何可以使用查询的任何内容,可以在此处找到更多信息How to get GET (query string) variables in Express.js on Node.js?如果您想要一种hacky解决方案,您可以向服务器发送xHTTP请求并解析响应
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var data = JSON.parse(xhttp.responseText);
console.log(data)
}
}
xhttp.open("POST", '/search', true);
xhttp.send();
</script>