0

I have following HTML form

 <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>

Phone schema

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;

For example,I have next phone in my database:

{
    "_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
}

Post request handler

    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(){
        .......

})});

If company,modelname,numbername in this form are same with same database fields then I have to render page with all info from database( company + picture + price for example).How I can compare form data and database data?

mex111
  • 77
  • 1
  • 1
  • 8

1 Answers1

0

How I understand it you are trying to pass the database info back to the HTML page. I think that this is a similar question to here: How do I serve partially dynamic HTML pages with Express? If you need to filter anything you could do that with queries of which more information could be found here How to get GET (query string) variables in Express.js on Node.js? If you want a kind of hacky solution you can send a xHTTP request to your server and parse the response

<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>
Ben
  • 244
  • 1
  • 14