[英]Invalid object, expected stream
I am getting this error我收到此错误
Type 'never' must have a 'Symbol.iterator' method that returns an iterator.
类型 'never' 必须有一个返回迭代器的 'Symbol.iterator' 方法。
I know that error is related to my if statement in the code below (drivers.service.ts)我知道该错误与下面代码中的 if 语句有关(drivers.service.ts)
export class DriversService { user: User; comp = ''; constructor(private db: AngularFirestore, private auth: AuthService, private afAuth: AngularFireAuth) { } getUsers() { return this.auth.user.pipe( take(1), map(user => { // user['company']; this.comp = user['company']; return user['company']; }) ) } findUser(value) { if(this.comp.length <= 0){ return this.getUsers().subscribe(user => { this.comp = user; console.log('Bye', this.comp) return this.comp; }); } let email = this.db.collection(`companies/${this.comp}/users`, ref => ref.where('email', '==', value)).valueChanges({ idField: 'id' }).pipe( take(1) ); let name = this.db.collection(`companies/${this.comp}/users`, ref => ref.where('name', '==', value)).valueChanges({ idField: 'id' }).pipe( take(1) ); return [email, name]; }
And accutal error is showing in add-driver.page.ts in forkJoin()并且在 forkJoin() 的 add-driver.page.ts 中显示了实际错误
export class AddDriverPage implements OnInit { users = []; participant = ''; form: FormGroup; constructor(private driverService: DriversService, private loadingCtrl: LoadingController, private auth: AuthService) { } ngOnInit() { } addDriver() { let obs = this.driverService.findUser(this.participant); forkJoin(obs).subscribe(res => { for (let data of res) { if (data.length > 0) { this.users.push(data[0]); } } console.log('it works:', this.participant) this.participant = ''; }); } }
So how do I solve this issue?那么我该如何解决这个问题呢?
Change in your service function:更改您的服务 function:
if (this.comp.length <= 0) {
return [this.getUsers().pipe(map(user => {
this.comp = user;
console.log('Bye', this.comp);
return this.comp;
}))];
}
Then in your component:然后在您的组件中:
addDriver() {
const obs = this.driverService.findUser(this.participant);
forkJoin(obs).subscribe(res => {
if (!res || !res.length) {
return console.log('Got undefined');
}
for (let data of res) {
if (data.length > 0) {
this.users.push(data[0]);
}
}
console.log('it works:', this.participant)
this.participant = '';
}, error => {
console.log('Here you got an error: ', error);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.