node.jsangularmean-stackexpr

How to send form value to node/express to search database?


I've got a pre populated form with gps coordinates and I am trying to search a database.




<form>

<mat-toolbar  color="primary">

  <mat-toolbar-row style="justify-content: center;" id="test">  Yonder   </mat-toolbar-row>
 <br>


  <mat-toolbar-row style="justify-content: center;">  <input id="geoSearch" type="text" value="{{coords}}">  </mat-toolbar-row>
  <mat-toolbar-row style="justify-content: center;">    <button type="submit" class="subMitButton" id="location-button" mat-raised-button onclick="window.location.reload();" color="accent"> Locate </button>   </mat-toolbar-row>


</mat-toolbar>
<br>


</form>



and my backend code


app.get('/api/animals',(req, res, next) => {

Animal.find({})
.then(documents => {
  res.status(200).json({
    message: 'success',
    animals: documents
  });
});



How can I send the data from the form to be used in the find query?

Thanks!


Solution

  • Use HttpClient to make the request to your endpoint. In this case you could have something like:

    public response: any;
    
    constructor(private readonly http: HttpClient) {}
    
    public makeRequest(): void {
      this.http.get('XXXXXXXXX/api/animals').subscribe((response) => {
        this.response = response;
        // console.log(response)
      })
    }

    Where XXXXXXXXXXXX is your api base url (probably something like http://localhost:3000)