Thursday, May 9, 2024
 Popular · Latest · Hot · Upcoming
113
rated 0 times [  118] [ 5]  / answers: 1 / hits: 21074  / 7 Years ago, sun, may 21, 2017, 12:00:00

ERROR:



core.es5.js?0445:1084 ERROR SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>





SITUATION:



What I am trying to achieve is to pre-select the choice the user already made previously and prevent him from voting twice.






CODE:



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>


component.ts



votes: any;

ngOnInit() {
this.pollService.voted(this.poll, localStorage.getItem('userId')).subscribe(
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); }
);
}

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


service



voted(poll: Poll, userID: string) {
return this.http.get('http://localhost:3000/'+userID)
.map(response => response.json());
}


routes/user.js



router.get('/:userid', function (req, res, next) {
var userId = req.params.userid;
User.findById(userID, function (err, user) {
console.log(USER JSON? :+user.json());
return res.send(user.json());
});
});


models/user



var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var mongooseUniqueValidator = require('mongoose-unique-validator');

var schema = new Schema({
firstName: {type: String, required: true},
lastName: {type: String, required: true},
password: {type: String, required: true},
email: {type: String, required: true, unique: true},
polls: [{type: Schema.Types.ObjectId, ref: 'Poll'}],
votes: [{
poll: {type: Schema.Types.ObjectId, ref: 'Poll'},
choice: {type: Number},
}],
});

schema.plugin(mongooseUniqueValidator);

module.exports = mongoose.model('User', schema);

More From » node.js

 Answers
7

This error happens when server responds with HTML instead of JSON, particularly when a route is not found and the response is 404 page.



It's unlikely that API has the only route which is mounted at root and available as http://localhost:3000/'+userID, just because it's not possible for the server to do anything else in this case.



If the route is mounted to some path, like



app.use('/user', userRoutes);


then the requests should be performed to this path as well



return this.http.get('http://localhost:3000/user/'+userID)

[#57714] Thursday, May 18, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
wilson

Total Points: 27
Total Questions: 93
Total Answers: 93

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
wilson questions
Tue, Aug 9, 22, 00:00, 2 Years ago
Wed, May 11, 22, 00:00, 2 Years ago
Wed, May 20, 20, 00:00, 4 Years ago
Wed, May 13, 20, 00:00, 4 Years ago
;