使用take(1)

时间:2018-04-15 22:54:08

标签: angular rxjs observable

我在使用firestore的角度服务中有以下代码,没有语法错误,它工作正常,

问题是返回类型,因为我使用take(1)我希望返回类型为Observable<Client>

fetchByUserId(uid: string): Observable<Array<Client>> {
    return this.collection(ref => ref.where(`users.${uid}`, '>', 0))
      .valueChanges()
      .take(1);
  }

我尝试使用.first(),但问题是使用first observable完成,我想继​​续流式传输。

1 个答案:

答案 0 :(得分:1)

在你的情况下,

.first()和.take(1)实际上会做(almost)同样的事情。

您想要的是将结果数组映射到数组的第一个元素。

你应该可以插入这样的地图:

function changeDir(d, v, min, max) {
  return v > max || v < min ? -d : d;
}

function draw() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.fillStyle = col;
  ctx.fillRect(x, y, w, h);

  // Change direction if necessary
  dx = changeDir(dx, x, 0, 150);
  dy = changeDir(dy, y, 0, 150);
  dw = changeDir(dw, w, 0, 300);
  dh = changeDir(dh, h, 0, 300);

  // Accumulate
  x += dx;
  y += dy;
  w += dw;
  h += dh;
}

这假设this.collection(...)实际返回return this.collection(...) .map(arr => arr[0]) ,在这种情况下,此Observable<Array<Client>>的结果将返回map

相关问题