SITUATION:
我真的不知道是什么导致了这个错误 .
但我知道它起源于何处 . 相关代码如下 .
我做错了什么?
编辑:添加路由器代码 . 如果您认为有任何相关内容遗失,请告诉我 .
ERROR:
core.es5.js?0445:1084 ERROR TypeError: Cannot read property 'poll' of undefined
CODE:
service
voteOn(poll: Poll, userID: string, choice: number) {
var user;
this.http.get('http://localhost:3000/user/'+userID)
.map(response => response.json())
.subscribe(
json => {
user = JSON.parse(json),
console.log("USER :"+user);
if (user.votes == undefined) {
user.votes = [{poll, choice}];
} else {
user.votes.push({poll, choice});
}
const body = user;
const headers = new Headers({'Content-Type': 'application/json'});
const token = localStorage.getItem('token')
? '?token=' + localStorage.getItem('token')
: '';
return this.http.patch('http://localhost:3000/user/'+token, body, {headers: headers})
.map((response: Response) => response.json())
.catch((error: Response) => {
this.errorService.handleError(error);
return Observable.throw(error);
})
.subscribe();
}
)
}
poll.component.ts
import { Component, Input } from "@angular/core";
import { Poll } from "./poll.model";
import { PollService } from "./poll.service";
@Component({
selector: 'app-poll',
templateUrl: './poll.component.html',
styles: [`
.author {
display: inline-block;
font-style: italic;
font-size: 12px;
width: 80%;
}
.config {
display: inline-block;
text-align: right;
font-size: 12px;
width: 19%;
}
.panel {
width: 600px;
margin: auto;
margin-top: 30px;
}
.panel-body {
padding: 20px;
}
`]
})
export class PollComponent {
@Input() poll: Poll;
constructor(private pollService: PollService) {}
votes: any;
ngOnInit() {
this.pollService.voted(this.poll, localStorage.getItem('userId')).subscribe(
data => {
console.log("DATA:" + data);
this.votes = data.votes;
console.log("NGONINIT this.votes: "+ this.votes);
},
err => { console.log("NGONINIT ERROR: "+ err) },
() => { console.log("SUBSCRIBE COMPLETE this.votes: "+ this.votes); }
);
}
onEdit() {
this.pollService.editPoll(this.poll);
}
onDelete() {
this.pollService.deletePoll(this.poll)
.subscribe(
result => console.log(result)
);
}
onChoice1() {
this.pollService.increaseCounter1(this.poll);
this.onVote1();
}
onChoice2() {
this.pollService.increaseCounter2(this.poll);
this.onVote2();
}
onVote1() {
this.pollService.voteOn(this.poll, localStorage.getItem('userId'), 1);
}
onVote2() {
this.pollService.voteOn(this.poll, localStorage.getItem('userId'), 2);
}
belongsToUser() {
return localStorage.getItem('userId') == this.poll.userId;
}
alreadyVotedFor(choice: number) {
let result = "";
if (this.votes) {
console.log("THIS.VOTES: "+this.votes);
for (var i = 0; i < this.votes.length; i ++) {
if (this.votes[i].poll == this.poll.pollId) {
result = "disabled";
if (this.votes[i].choice == choice) {
result = "selected";
}
}
}
}
return result;
}
}
poll.component.html
<article class="panel panel-default">
<div class="panel-body">
{{ poll.title }}
<br>
<br>
<form #form="ngForm">
<fieldset [disabled]="alreadyVotedFor(-1)">
{{ poll.counter1 }} votes <input type="radio" id="{{ poll.choice1 }}" name="my_radio" value="{{ poll.choice1 }}" (click)="onChoice1(form)" [checked]="alreadyVotedFor(1)"> {{ poll.choice1 }}
<br>
{{ poll.counter2 }} votes <input type="radio" id="{{ poll.choice2 }}" name="my_radio" value="{{ poll.choice2 }}" (click)="onChoice2(form)" [checked]="alreadyVotedFor(2)"> {{ poll.choice2 }}
</fieldset>
</form>
</div>
<footer class="panel-footer">
<div class="author">
{{ poll.username }}
</div>
<div class="config" *ngIf="belongsToUser()">
<a (click)="onEdit()">Edit</a>
<a (click)="onDelete()">Delete</a>
</div>
</footer>
</article>
user.model
import { Poll } from '../polls/poll.model';
export class User {
constructor(public email: string,
public password: string,
public votes?: [{poll: Poll, choice : number }],
public firstName?: string,
public lastName?: string
) {}
}
routes/user
router.patch('/', function (req, res, next) {
var decoded = jwt.decode(req.query.token);
User.findById(decoded.user._id, function (err, user) {
user.votes = req.body.votes;
user.save(function(err, result) {
if (err) {
return res.status(500).json({
title: 'An error occurred',
error: err
});
}
res.status(201).json({
poll: 'Vote Saved',
obj: result
});
});
});
});
1 回答
您的代码没有完全显示问题 . 我能够使用上一个问题的在线演示找出问题所在 .
该错误发生在
ErrorService
中 . 更确切地说