【问题标题】:how to take an array of 8 digit strings as input in angular and pass to node.js backend?如何将 8 位字符串数组作为角度输入并传递给 node.js 后端?
【发布时间】:2019-11-11 13:41:25
【问题描述】:

我正在尝试将 8 位字符串数组作为 angular 的输入,并将其作为参数传递给 node.js 端点,如下所示,如果我只有一个字符串,下面的效果很好,如下面的硬编码所示req.query.params 的数组字符串在 node.js api 中效果很好,但是如何将 8 位字符串数组作为输入并传递给 node.js?

在html中:

<textarea rows="6" [(ngModel)]="enteredValue"></textarea>
<hr>
<button (click)="get_radar_lifecycle_data()">Search</button>

在component.ts中

get_radar_lifecycle_data(){
     const params = new HttpParams().set('params', this.enteredValue);
     this.http.get('http://localhost:3000/api/radar_life_cycle3',{params})
     .subscribe(response => {
          console.log(response);
          this.newPost = response
      });
}

node.js

数组的硬编码字符串到req.query.params效果很好

app.get("/api/radar_life_cycle3", (req, res, next) => {
  console.log(req.query.params)
   Radar_life_cycle.find({orgRadar: {$in:["51509646","51643617"]}})
   .then(documents => {
      res.status(200).json({
        message: "Posts fetched successfully!",
        posts: documents
      });
    });
  });

实际的api调用

app.get("/api/radar_life_cycle3", (req, res, next) => {
  console.log(req.query.params)
   Radar_life_cycle.find({orgRadar: {$in:req.query.params}})
   .then(documents => {
      res.status(200).json({
        message: "Posts fetched successfully!",
        posts: documents
      });
    });
  });

【问题讨论】:

  • 尝试使用put调用get
  • 为什么用 put 而不是 get?这将如何解决这个问题?

标签: javascript node.js angular mean-stack


【解决方案1】:

您应该使用.find({orgRadar: {$in:req.query.params.split(',')}}),因为它来自 FE,以这种逗号分隔值格式,并将其转换为字符串数组 split(',') 应该可以正常工作

【讨论】:

  • 如何保证用户以这种逗号分隔的格式输入?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多