I am currently working on updating a collection document by adding an object that includes a 'username' property along with a 'rating' value. This particular object is supposed to be pushed into the existing 'ratings' array within the document.
Strangely, the 'username' property seems to be interpreted as a string rather than its expected variable value. On the other hand, the 'rating' value is correctly retrieved and added to the array.
Template.Rating.events({
'submit form': function (event, template) {
event.preventDefault();
var form = template.find('form');
var rating = template.find('input[name="rating"]:checked').value;
var currentPun = Session.get('randomPun');
var username = Meteor.user().username || null;
console.log(username);
Puns.update(
{ _id: currentPun._id},
{
$push: {
// The issue arises when pushing to the ratings array; the {username: rating} object interprets 'username' as string instead of a variable.
ratings: {username: rating}
}
}
);
}
});
I find it perplexing that the correct username is displayed in the console log halfway through the code.
Due to this discrepancy, the 'ratings' array within the collection document appears like this:
[{username: 3}, {username: 5}, {username: 2}, {username: 4}]
Instead of appearing as intended:
[{joesmith: 3}, {janedoe: 5}, {kevincostner: 2}, {donaldtrump: 4}]