【问题标题】:How to send form value to node/express to search database?如何将表单值发送到节点/快递到搜索数据库?
【发布时间】:2020-10-27 00:52:21
【问题描述】:

我有一个带有 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
  });
});



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

谢谢!

【问题讨论】:

  • onclick="window.location.reload();....?
  • 使用 angulars http 发出 ajax 请求

标签: node.js angular mean-stack expr


【解决方案1】:

使用 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 基本网址(可能类似于 http://localhost:3000)

【讨论】:

  • 谢谢!我得到了使用 http 请求的想法,但是如何捕获我在搜索字段中输入的数据?
  • @HMCS 在这种情况下,您需要在输入字段上使用 formControl 之类的东西来获取字段的值。确保您已将ReactiveFormsModule 添加到App.Module.ts 文件中的导入中,然后在您的组件上使用&lt;input id="geoSearch" type="text" [formControl]="myControl"&gt;。在 ts 文件中添加一个变量 public myControl: FormControl = new FormControl(''),现在您需要访问输入中的值是 this.myControl.value。希望这会有所帮助
猜你喜欢
  • 1970-01-01
  • 2018-10-09
  • 1970-01-01
  • 2011-07-02
  • 2020-08-18
  • 2019-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多