I have a collection of websites with the ability to up or down vote them. In the collection is an empty array to keep track of user that have already voted by inserting the userId in to the array
Problem: I can insert the userID or even the username but it only store the id/name variable as a string for that time. If I click to vote again the previous element becomes an integer. It seems that the strings only remain if I put "" around them. But I'm using a variable to get the userid/name. When I put "" around it I literally get "theVariable", not helpful. I've tried all I can think of....any help is appreciated.
schema
{
url:url,
title:title,
description:description,
createdOn:new Date(),
createdBy:Meteor.user()._id,
votes: 0,
upVotes: 0,
downVotes: 0,
voted: []
}
Code
"click .js-downvote":function(event){
// access the id for the website in the database
var website_id = this._id;
console.log("Down voting website with id "+website_id);
//add a down vote
var user = Meteor.user()._id;
if (user){
var theUser = this.voted.push(user);
Websites.update({_id:website_id}, {$inc: {votes: 1}}, {$inc:{downVotes: -1 }}, {$push:{voted: theUser}});
}
console.log(this.voted);
return false;// prevent the button from reloading the page
}
Output I get at the console each time I clcik:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "mWe4r2JcfhRAvn97y"]
First I get the string, then for each subsequent click the string moves down and the first element becomes a number. I need the userid to store each time. So I can check against it for allowing voting.
.update()statements as they all should be in one, and really with a check that the user is not already in thevotedarray:.update({ "_id": website_id, "voted": { "$ne": Mereor.user()._id } },{ "$inc": { "votes": 1, "downvotes": -1 }, "$push": { "voted": Meteor.user()._id } })Also noting that you don't want to be "pushing" an "array", but just the item. Not sure what your thinking is around keeping another array than on the updated item either. Bit confusing where your problem is here.