我在 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
所在的参数undefined
,followButtonClick()
因此它会尝试获取this.status[undefined].followers++;
并抛出无法找到属性followers
的错误undefined
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句