Hellow there. thanks for clicking on this post. i would like to know how do i get/query data from mongodb based on the input field. example is if i were to input "USA" in the input field named" location" it will only get data that has USA in it and the data will be displayed on the next page. How do i get this to work when i click on a button. i will link some screenshots of my webite and code(i have a feeling my code is completely wrong.)
error im facing after pasting ur code and code for passing data
this is my home.component.html line40. it is to pass the data to next page
this is the page where the data will be displayed
here is some codes ive tried to mess around with(havent tested if it works)
//this is in my api.js file
//get data from "listing"(which is my db collection) based on "inputcountry( the inputfield)"
router.get('/home', function(req, res) {
db.collection('listing').find({"inputCountry": req.body.inputCountry}).toArray( (err, results) => {
console.log('got search')
res.send(results)});
});
//this is in my service .ts file (named getservice)
//this is for the searching of house based on location
searchHouse() {
return this.http.get<any[]>('./api/home');
}
//this is in my home. component.ts file (where the searching will happen)
constructor(private fb: FormBuilder, private router: Router, private getService: GetService) {
// Retrieve posts from the API
this.getService.searchHouse().subscribe(searchedhouses => {
this.searchedhouses = searchedhouses;
});
}
<!--this is in my home.component.html where there is the input field named "inputcountry" and submit button -->
<form [formGroup] = "myForm" (ngSubmit) = "searchHouse (myForm)">
<div class="form-group">
<label for="inputCountry">Enter Country</label>
<input type="text" class="form-control" formControlName="inputCountry" id="inputCountry" name = "inputCountry" placeholder="Location e.g Singapore .">
</div>
<button class="btn btn-lg btn-primary btn-block text-uppercase" routerLink="/houses" type="submit" >Find</button>
</form>