Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.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 方法some()不';t在第8节中不能按预期工作_Javascript_Angular - Fatal编程技术网

Javascript 方法some()不';t在第8节中不能按预期工作

Javascript 方法some()不';t在第8节中不能按预期工作,javascript,angular,Javascript,Angular,我有一个Angular应用程序,我想为用户添加follow/unfollow功能。我正在尝试添加IsFollow标志,这样我就能够知道用户是否被跟踪,并根据这一点,我将显示两个不同的按钮:跟踪和取消跟踪。为此,我正在使用some()方法,但它不起作用。它告诉我isFollowed标志是未定义的,尽管它应该显示true或false。我不明白问题出在哪里,这是我的HTML相关部分: <button *ngIf="!isFollowing; else unfollowBtn" class="bt

我有一个Angular应用程序,我想为用户添加follow/unfollow功能。我正在尝试添加IsFollow标志,这样我就能够知道用户是否被跟踪,并根据这一点,我将显示两个不同的按钮:跟踪取消跟踪。为此,我正在使用
some()
方法,但它不起作用。它告诉我isFollowed标志是
未定义的
,尽管它应该显示
true
false
。我不明白问题出在哪里,这是我的HTML相关部分:

<button *ngIf="!isFollowing; else unfollowBtn" class="btn" id="btn-follow" (click)="follow(id)">Follow </button>
<ng-template #unfollowBtn><button class="btn" id="btn-follow" (click)="unFollow(id)">Unfollow</button></ng-template>
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from "@angular/router";

import { AuthenticationService } from '@services/auth.service';
import { FollowersService } from '@services/followers.service';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit {
  user;
  id;
  followers;
  isFollowing: boolean;

  constructor(
    private authenticationService: AuthenticationService,
    private followersService: FollowersService,
    private router: Router,
    private route: ActivatedRoute,
  ) { }

  ngOnInit() {
    this.id = this.route.snapshot.paramMap.get("id");
    this.authenticationService.getSpecUser(this.id).subscribe(
      (info => {
        this.user = info;
      })
    );
    this.followersService.getFollowing().subscribe(
      data => {
        this.followers = data;
        this.isFollowing = this.followers.some(d => d.id == this.user.id);
      }
    );
  }

  follow(id) {
    console.log('follow btn');
    this.followersService.follow(id).subscribe(
      (data => console.log(data))
    )
    this.isFollowing = true;
  }

  unFollow(id) {
    console.log('unFollow btn');
    this.followersService.unFollow(id).subscribe(
      (data => console.log(data))
    )
    this.isFollowing = false;
  }
}

任何帮助都将不胜感激。

如果您希望每次调用它,并确保
此用户已填充。然后你可以用叉形连接

forkJoin(
    this.authenticationService.getSpecUser(this.id),
    this.followersService.getFollowing()
  ).pipe(
    map(([info, data]) => {
      // forkJoin returns an array of values, here we map those values to the objects
      this.user = info; 
      this.followers = data;                               
      this.isFollowing = this.followers.some(d => d.id == this.user.id);
    })
  );
没有测试这个,因为我没有时间。如果你做一次闪电战,我们可以看到它的行动,并从那里尝试


希望这能有所帮助。

如果您希望每次调用它,并确保
已填充此.user
。然后你可以用叉形连接

forkJoin(
    this.authenticationService.getSpecUser(this.id),
    this.followersService.getFollowing()
  ).pipe(
    map(([info, data]) => {
      // forkJoin returns an array of values, here we map those values to the objects
      this.user = info; 
      this.followers = data;                               
      this.isFollowing = this.followers.some(d => d.id == this.user.id);
    })
  );
没有测试这个,因为我没有时间。如果你做一次闪电战,我们可以看到它的行动,并从那里尝试


希望这能有所帮助。

什么类型的
追随者
?它是数组吗?是否确定数据未定义?是否确认
this.user.id
具有您期望的值。而
data
也有您期望的值?Array.isArray(followers)
返回什么?您进行两次异步调用
this.user
仅当
getSpecUser
getFollowing
之前完成时才会初始化。什么类型的
followers
?它是数组吗?是否确定数据未定义?是否确认
this.user.id
具有您期望的值。而
data
也有您期望的值?Array.isArray(followers)返回什么?您进行两次异步调用
this.user
仅当
getSpecUser
getFollowing
之前完成时才会初始化。这里的正确答案是forkJoin。应始终避免嵌套订阅。@bryan60我同意。我甚至不知道我在想什么。forkJoin是这里的正确答案。应始终避免嵌套订阅。@bryan60我同意。甚至不知道我在想什么。