错误类型错误:无法读取未定义的属性

死人

我在 Angular 6 中有应用程序,用户可以关注、喜欢、不喜欢等。我正在尝试通过 post 方法将数据保存到服务器。

当用户点击例如关注时,我收到以下错误:

UserProfileComponent.html:27 ERROR TypeError: Cannot read property 'followers' of undefined
    at UserProfileComponent.push../src/app/user-profile/user-profile.component.ts.UserProfileComponent.followButtonClick (user-profile.component.ts:46)
    at Object.eval [as handleEvent] (UserProfileComponent.html:27)
    at handleEvent (core.js:19324)
    at callWithDebugContext (core.js:20418)
    at Object.debugHandleEvent [as handleEvent] (core.js:20121)
    at dispatchEvent (core.js:16773)
    at core.js:17220
    at HTMLButtonElement.<anonymous> (platform-browser.js:988)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
    at Object.onInvokeTask (core.js:13842)

这是服务器中的json文件:

{
  "statuses": [{
    "id": 1,
    "statusId": 2,
    "likes": 121,
    "following": 723,
    "followers": 4433
  }]
}

这是我的服务:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {Status } from '../model/statuses.model';
import { Comment } from '../model/comments.model';

@Injectable({
  providedIn: 'root'
})
export class UserService {
  status: Status[];
  constructor(private http: HttpClient) { }
  statusUrl = 'http://localhost:3000/statuses';
  commentsUrl = 'http://localhost:3000/comments';

  getStatuses() {
    return this.http.get<Status[]>(this.statusUrl);
  }

  addStatus(status: Status) {
   return this.http.patch(this.statusUrl, status);
  }

  addComments(comment: Comment) {
    return this.http.post(this.commentsUrl, comment);
  }

}

这是组件 ts

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { UserService } from '../service/user.service';
import { Status } from '../model/statuses.model';
import { Comment } from '../model/comments.model';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-user-profile',
  templateUrl: './user-profile.component.html',
  styleUrls: ['./user-profile.component.scss']
})
export class UserProfileComponent implements OnInit {
  status: Status[];
  comment: Comment[];
  numberOflikes = 121;
  numberOffollowing = 723;
  numberOffollowers = 4433;

  constructor(
    private formBuilder: FormBuilder,
    private http: HttpClient,
    private userService: UserService
  ) {}

  addForm: FormGroup;

  ngOnInit() {
    this.addForm = this.formBuilder.group({
      id: [],
      name: ['', Validators.required],
      city: ['', Validators.required],
      description: ['', Validators.required],
    });

    this.userService.getStatuses()
      .subscribe(data => {
        this.status = data;
        console.log(data);
      });
  }

  addComments() {
    this.userService.addComments(this.addForm.value)
      .subscribe(data => {
        this.comment.push(this.addForm.value);
      });
  }

  followButtonClick(statusId) {
    this.status[statusId].followers++;
    this.persistStatus(this.status[statusId]);
  }

  persistStatus(status) {
    this.userService.addStatus(status);
  }

}

这是 html

哈维幽灵

美国纽约
      </div>
      <ul class="profile_card-bottom" *ngFor="let stat of status">
        <li class="likes">
          <span class="assets-count">{{stat.followers}}</span>
          <span class="assets-title">Likes</span>
        </li>
        </ul>
</div>

这是状态模型

export class Status {
    id: number;
    statusId: number;
    like: number;
    following: number;
    followers: number;
}

我的代码做错了什么?

阿乔姆·阿米良

正如我所看到的,您没有followButtonClick()在 html 中将参数传递给您的方法,因此在*ngFor循环中移动您的按钮stat.id按照@selemmn 所述传递

<h1>
 Harvey Specter
 <span class="heart reaction">
  <i class="fa fa-heart heart" aria-hidden="true"(click)="followButtonClick(stat.id)"></i>
 </span>
</h1>

并将您的followButtonClick()方法更改为此

followButtonClick(statusId) { 
 const statusToUpdate = this.status.filter(status => status.id === statusId)[0]; 
 statusToUpdate.followers++; 
 this.persistStatus(statusToUpdate); 
}

因此,由于您没有传递您statusId所在的参数undefinedfollowButtonClick()因此它会尝试获取this.status[undefined].followers++;并抛出无法找到属性followers错误undefined

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

类型错误:未定义无法读取属性“webpackJsonp”

角度:错误类型错误:无法读取未定义的属性___

AngularJS类型错误:无法读取未定义的属性“ GET()”

opencv - 类型错误:无法读取未定义的属性“长度”

错误类型错误:无法读取未定义的属性“标题”

错误类型错误:无法读取未定义的属性“...”

错误类型错误:无法读取未定义的属性“填充”

类型错误:无法读取未定义的属性“indexOf”,无法读取未定义的属性“toLowerCase”

错误类型错误:无法读取未定义的属性“stateName”

错误类型错误:无法读取未定义的属性“getUsers”

“类型错误:无法读取未定义的属性‘名称’”错误

类型错误:无法读取未定义的属性“类型”

错误类型错误:无法读取未定义的属性“列表”

类型错误:无法读取未定义的属性“ChangeClassName”

类型错误:无法读取未定义的属性“_alreadyWrapped”

错误类型错误:无法读取未定义的属性“_id”

类型错误:无法读取未定义 NodeJS 的属性“样式”

渲染错误:“类型错误:无法读取未定义的属性‘’”

类型错误:无法读取未定义错误的属性“_id”

类型错误:无法读取未定义的属性“querySelector”

错误类型错误:无法读取未定义的属性“doc”

类型错误:无法在 setTimeout() 中读取未定义的属性“then”

**错误** 类型错误:无法读取未定义的属性“替换”

类型错误:无法读取未定义的属性(读取“图像”)

类型错误:无法读取未定义的属性(读取“缓存”)

类型错误:无法读取未定义的属性(读取“暗”)

类型错误:无法读取未定义的属性(读取“reduce”)

类型错误:无法读取未定义的属性(读取“类型”)

类型错误:无法读取未定义读取“2”的属性