有什么办法可以将两个集合合并为一个集合并做出响应?

时间:2019-07-29 06:08:39

标签: node.js angular mongoose

我正在尝试执行两个查找查询并将结果组合到一个数组中。

这是我的代码,它不起作用

if (req.query.materialNumber) {
  MaterialStockItem.find({
    componentN: req.query.materialNumber,
    itemType: 'material'
  }, function(err, docs) {
    if (err)
      throw err;
    var matStock = docs;
    StockItem.find({
        componentN: req.query.materialNumber,
        itemType: 'material'
      }),
      function(err, docs2) {
        if (err)
          throw err;
        var stock = docs2
        var combinedArray = matStock.concat(stock)
        res.json(combinedArray)
      }
  })
}

1 个答案:

答案 0 :(得分:0)

也许您可以使用forkJoin

示例

GetCursorPos(&cursorPos);
CComPtr<IUIAutomationElement> elFromPoint;
_automation->ElementFromPoint(cursorPos, &elFromPoint);

因此将代码更改为

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { forkJoin } from 'rxjs';

@Injectable()
export class DataService {

  constructor(private http: HttpClient) { }

  public requestDataFromMultipleSources(): Observable<any[]> {
    let response1 = this.http.get(requestUrl1);
    let response2 = this.http.get(requestUrl2);
    let response3 = this.http.get(requestUrl3);
    // Observable.forkJoin (RxJS 5) changes to just forkJoin() in RxJS 6
    return forkJoin([response1, response2, response3]);
  }
}