2017-05-21 2 views
-1

SITUATION lesen:ERROR Typeerror: kann Eigenschaft ‚Umfrage‘ undefinierter

Ich weiß nicht wirklich wissen, was diesen Fehler verursacht.

Aber ich weiß woher es stammt. Relevanter Code unten.

Was habe ich falsch gemacht?

EDIT: Router-Code hinzugefügt. Sag mir, wenn du denkst, dass etwas Wichtiges fehlt?


ERROR:

core.es5.js?0445:1084 ERROR TypeError: Cannot read property 'poll' of undefined 

enter image description here


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

Routen/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 
      }); 
     }); 
    }); 
}); 
+0

Ihre PATCH-Anfrage hat einen internen Serverfehler direkt davor. –

+0

@Pengyy Es ist Kurzschrift '{Poll: Wahl: Wahl}' –

+0

@NeilLunn oh, ist das neue Feature? Entschuldigung, das weiß ich vielleicht nicht. – Pengyy

Antwort

1

Ihr Code nicht vollständig das Problem zeigen. Ich konnte das Problem anhand Ihrer Online-Demo aus der vorherigen Frage herausfinden.

Der Fehler tritt in ErrorService auf. Genauer

const errorData = new Error(error.title, error.error.poll); 
+0

Danke! Ich hatte den Fehlerdienst vergessen. – Coder1000

Verwandte Themen