按名称过滤Firestore数据

时间:2019-03-18 22:28:30

标签: angular nativescript nativescript-firebase

我正在使用本地脚本Angular构建社交应用,现在尝试添加“搜索”用户界面,以便用户可以按名称搜索其他用户 我已经将用户存储在firebase firestore中,并且我想按名称搜索他们,但是,但是我收到了.subscribe错误,例如

error TS2339: Property 'subscribe' does not exist on type 'void'

这是我的代码:

export class HomeComponent implements OnInit {

ageValue: number = 0;
searchValue: string = "";
items: Array<any>;
name_filtered_items: Array<any>;
constructor(

  public firebaseService: FirebaseService,

  private router: Router

) { }

ngOnInit() {

this.getData();

}
getData(){

this.firebaseService.getUsers()

.subscribe(result => {

  this.items = result;

  this.age_filtered_items = result;

  this.name_filtered_items = result;

})

}

viewDetails(item){
 this.router.navigate(['/details/'+ item.payload.doc.id])
}
capitalizeFirstLetter(value){

return value.charAt(0).toUpperCase() + value.slice(1);

}
 searchByName(){

let value = this.searchValue.toLowerCase();

this.firebaseService.searchUsers(value)

.subscribe(result => {

  this.name_filtered_items = result;

  this.items = this.combineLists(result, this.age_filtered_items);

})

}

combineLists(a, b){

let result = [];



a.filter(x => {

  return b.filter(x2 =>{

    if(x2.payload.doc.id == x.payload.doc.id){

      result.push(x2);

    }

  });

});

return result;
}

}

FirebaseService.ts:

export class FirebaseService {

constructor( private ngZone: NgZone,) {}

getUsers(){

    return firebase.firestore().collection('users').snapshotChanges();

}



searchUsers(searchValue){

    return firebase.firestore().collection('users',ref => ref.where('nameToSearch', '>=', searchValue)

        .where('nameToSearch', '<=', searchValue + '\uf8ff'))

      .snapshotChanges()

     }
}

2 个答案:

答案 0 :(得分:0)

这是我用angularfire2解决的方法

react-native-web

这是我的Firebase数据库的外观:

enter image description here

答案 1 :(得分:0)

nativescript-plugin-firebase不支持## Start ## x <- runif(100, -3, 3) y <- 2 * x + rnorm(100) res <- lm(y~x) ## End ## #' Polygonal linear regression #' @description plr is used to fit polygonal linear models. #' @export plr <- function(formula, data, model = TRUE, ...){ some_core... class(res) <- 'plr' res } ,您必须使用onSnapshot方法进行回调。

snapshotChanges()