简体   繁体   中英

how to take an array of 8 digit strings as input in angular and pass to node.js backend?

I am trying to take an array of 8 digit strings as input from angular and pass it as a parameter to node.js endpoint as follows,the following works great if I just have one string ,as you can see below hard coding string of arrays to req.query.params works great in the node.js api but how do I take an array of 8 digit strings as input and pass to node.js?

In html:

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

in 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

hard coding string of arrays to req.query.params works great

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

Actual api call

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM