Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.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 错误类型错误:无法读取属性';投票';未定义的_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 错误类型错误:无法读取属性';投票';未定义的

Javascript 错误类型错误:无法读取属性';投票';未定义的,javascript,angular,typescript,Javascript,Angular,Typescript,情况: core.es5.js?0445:1084 ERROR TypeError: Cannot read property 'poll' of undefined 我真的不知道是什么导致了这个错误 但我知道它是从哪里来的。相关代码如下 我做错了什么 编辑:添加路由器代码。如果你认为有什么相关的遗漏,请告诉我 错误: core.es5.js?0445:1084 ERROR TypeError: Cannot read property 'poll' of undefined 代码:

情况:

core.es5.js?0445:1084 ERROR TypeError: Cannot read property 'poll' of undefined
我真的不知道是什么导致了这个错误

但我知道它是从哪里来的。相关代码如下

我做错了什么

编辑:添加路由器代码。如果你认为有什么相关的遗漏,请告诉我


错误:

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


代码:

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

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;
    }

}
路由/用户

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
          });
      });
   });
});

您的代码没有完全显示问题。我可以通过你的在线演示解决这个问题

错误发生在
ErrorService
中。更准确地说

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

您的修补程序请求在此之前有一个内部服务器错误。@Pengyy它是缩写
{poll:poll,choice:choice}
@NeilLunn哦,这是新功能吗?抱歉,我可能不知道这一点。@NeilLunn的确,我认为这两个错误有点关联。以防万一,你会想:是的,这是我在控制台中得到的仅有的两个错误。请重新阅读。问题的关键是,在你有一个小的、独立的问题示例之前,你要删除一些东西。这几乎不是一个小的、独立的例子。