1

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.

3
  • Which line and which content is logging this to the console? Also, you don't need three .update() statements as they all should be in one, and really with a check that the user is not already in the voted array: .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. Commented Feb 26, 2016 at 6:45
  • I might supect your "moves down" is actually the pusing an array into an array behavior I just mentioned though. Commented Feb 26, 2016 at 6:46
  • So I included the log call and put my update calls all in one. The thing is when I have it in one it doesn't work. I split them up and it was fine. Commented Feb 26, 2016 at 7:14

1 Answer 1

0

Solution for:

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.

You can use string interpolation if you are using ES6 or higher versions of JS. You must use back-tick(`) and not single quotes(').

Example:

var name = "Rose";
console.log(`"My name is ${name}"`);

Output:

"My name is Rose"

In your code do this:

 Websites.update({_id:website_id}, {$push:{voted: `"${theUser}"`}});

It gets interpolated to:

Websites.update({_id:website_id}, {$push:{voted: "userid within quotes"}});

You will get the quote included. replace your query using this method

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I knew there was something with quotes to get it working right, couldn't remember and couldn't find the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.