如何将表单值发送到 node/express 以搜索数据库?

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

我有一个带有 gps 坐标的预填充表格,我正在尝试搜索数据库。




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



和我的后端代码


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

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



如何从表单发送数据以用于查找查询?

谢谢!

使用 HttpClient 向您的端点发出请求。在这种情况下,你可以有类似的东西:

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

XXXXXXXXXXXX 是您的 api 基础 url(可能类似于 http://localhost:3000)