我试图将8位字符串的数组作为输入,并将其作为参数传递给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
});
});
});
您应该使用 .find({orgRadar: {$in:req.query.params.split(',')}})
,因为它以这种逗号分隔的值格式来自fe,然后将其转换为字符串split(','(的数组,应正常工作