Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 云Firestore查询有时有效,但并不总是有效_Javascript_Angular_Firebase_Ionic3_Google Cloud Firestore - Fatal编程技术网

Javascript 云Firestore查询有时有效,但并不总是有效

Javascript 云Firestore查询有时有效,但并不总是有效,javascript,angular,firebase,ionic3,google-cloud-firestore,Javascript,Angular,Firebase,Ionic3,Google Cloud Firestore,这让我很困惑。我有一个在Firestore数据库中搜索项目的方法。当我直接从一次性测试调用该方法时,它就起作用了。当我使用完全相同的输入从我应用程序的另一部分调用该方法时,它不起作用 以下是执行搜索的方法: getProductsStartingWithCategory(textSoFar: string): Observable<Product[]> { console.log('searching for ', textSoFar); let endAt =

这让我很困惑。我有一个在Firestore数据库中搜索项目的方法。当我直接从一次性测试调用该方法时,它就起作用了。当我使用完全相同的输入从我应用程序的另一部分调用该方法时,它不起作用

以下是执行搜索的方法:

  getProductsStartingWithCategory(textSoFar: string): Observable<Product[]> {
    console.log('searching for ', textSoFar);
    let endAt = textSoFar + '\uf8ff';
    let filteredCollection: AngularFirestoreCollection<Product> =
      this.afs.collection('products', ref =>
        ref.orderBy('materialType').limit(30).startAt(textSoFar).endAt(endAt)
      );

    return filteredCollection.snapshotChanges().map(changes => {
      return changes.map(a => {
        console.log('matched one', a);
        const data = a.payload.doc.data() as Product;
        data.id = a.payload.doc.id;
        return data;
      });
    });
  }
同样,当我调用此方法时,我得到了预期的结果(将数据库中的产品与materialType“地毯”匹配)成功

但是,当我从应用程序的另一个页面使用该方法时,它不会返回任何结果。这个页面有点复杂——本质上,当用户输入发生变化时,就会调用该方法。以下是该方法的相关部分:

productCategoryChanged(productPurchase: ProductPurchase) {
if (productPurchase.materialType) {
  console.log('searching for products starting with "' + productPurchase.materialType + '"');

  let unsubscribe = this.productService.getProductsStartingWithCategory(productPurchase.materialType).subscribe(products => {
    products.forEach(product => {
      // logic goes here...
    });
  });
  // rest of method omitted
在这两种情况下,我都会在console.log消息中看到“搜索汽车”。搜索文本相同。我已经尝试过无数次使用不同的搜索文本(所有文本都在数据库中)。日志显示使用正确的输入调用该方法,但出于某种原因,我仅在从“test”方法调用它时才找到结果。为什么呢


我试过调整输入。我确实有另一个“产品”集合连接到一个可观察对象,但我认为这并不重要。我也使用了这个精确的策略进行“客户”搜索,效果很好。这个“产品”搜索几乎是一样的,但不起作用

你能显示完整的控制台日志吗?两种情况下“Car”的大小写是否相同?this.productService是否两次都处于相同的状态?这个问题看起来不太可能出现在你发布的代码中,所以它可能出现在你代码的其他地方。结果是一个错误,我没有取消订阅一个可观察的。
productCategoryChanged(productPurchase: ProductPurchase) {
if (productPurchase.materialType) {
  console.log('searching for products starting with "' + productPurchase.materialType + '"');

  let unsubscribe = this.productService.getProductsStartingWithCategory(productPurchase.materialType).subscribe(products => {
    products.forEach(product => {
      // logic goes here...
    });
  });
  // rest of method omitted