【问题标题】:Untyped function calls may not accept type arguments无类型函数调用可能不接受类型参数
【发布时间】:2018-12-30 07:31:24
【问题描述】:

我正在使用 Angular6 并尝试在此过滤器函数中获取文档内容。我的代码 sn-p 的最后一行给了我错误“无类型函数调用可能不接受类型参数”。不知道为什么我会收到这个错误...也许它不能在过滤器函数中使用...?

构造函数:

constructor(private afs : AngularFirestore, private authService : AuthService) { 
  //Get user collection on initialise
  this.userCollection = this.afs.collection('users');
}

过滤:

this.userCollection = this.afs.collection<User>('users', ref => {
      console.log(ref);
      return ref
    })

    this.usersOnSearch = this.userCollection.valueChanges();

    this.usersOnSearch.flatMap(user => {
      return user.filter(function(value) {
        let subjectTrue : boolean = false;
        let levelTrue : boolean = false;
        let docID : string = value.user_subjects.id;
        let userLevelPriceDocument :  AngularFirestoreDocument<UserSubjects>;
        userLevelPriceDocument = this.afs.doc<UserSubjects>(`user_subjects/${docID}`);
      })
    });

【问题讨论】:

  • 如果你删除 ,你有错误吗?我们也可以有你的构造函数吗?还有你的组件变量
  • 如果我删除 我不再收到错误消息。其他信息已更新。
  • 您似乎根本没有在 filter() 函数中返回任何内容 - 您是否应该返回 true/false 来指示是否应将给定用户包含在结果中?

标签: angular rxjs observable google-cloud-firestore angular6


【解决方案1】:

flatMap 签名要求返回 Observable&lt;T&gt; 类型。在您的情况下,您只返回&lt;T&gt;(在您的情况下是any,因为这是控制输出)。尝试用Observable.of() 包装返回值:

this.usersOnSearch.flatMap(user => {
  return Observable.of( user.filter(function(value) {
    let subjectTrue : boolean = false;
    let levelTrue : boolean = false;
    let docID : string = value.user_subjects.id;
    let userLevelPriceDocument :  AngularFirestoreDocument<UserSubjects>;
    userLevelPriceDocument = this.afs.doc<UserSubjects>(`user_subjects/${docID}`);
  })
  )
});

【讨论】:

  • 不幸的是,这不起作用。如果我删除了@Wandrille 提到的 ,那么它会消除错误
  • 我认为这是正确的方法,但对于 Angular 6 / rxjs 6,应该是“of”而不是“Observable.of”。
猜你喜欢
  • 2018-03-02
  • 2018-06-22
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 2021-01-19
  • 1970-01-01
  • 2014-02-23
  • 2016-07-02
相关资源
最近更新 更多