Note that there are some explanatory texts on larger screens.

plurals
  1. POCollection.allow update is getting null as an argument for doc (the preposed document to change)
    text
    copied!<p>I am trying to update a collection from the client using the Collection.allow functionality. I am trying to verify that the user isn't doing something bad, except I am getting null for an argument of what the doc should be.</p> <p>Server Code</p> <pre><code>Comments = new Meteor.Collection("comments"); if(Meteor.isServer){ Comments.allow({ insert: function (userId, doc) { return userId === doc.owner; }, update: function (userId, doc, fields, modifier) { console.log("UPDATING doc: "+ JSON.stringify(doc)); // NULL GAAARNGAD _(modifier).each( function( value, key, modifier ) { // for each modifier console.log(JSON.stringify(key) + " - " + JSON.stringify(value)); var ch_feilds = _.keys(value); // array of fields that are requested to be changed. var vote_feilds = ['upvoters', 'downvoters']; // feilds that can be changed by pull and addToSet var count_feilds = ['upc', 'downc', 'score']; switch(key){ case "$pull": case "$addToSet": // If adding there are fields attempted to be modified by pull or addtoset that aren't // in the allowed fields, reject update if(_.union(ch_feilds, vote_feilds).length != vote_feilds.length) return false; // make sure they aren't attempting to vote for someone else. for(var i = 0; i &lt; ch_feilds.length; i++) if(ch_feilds[i] != userId) return false; break; case "$set": // If the union of feilds to chang and the feilds set can change yeilds larger results // than allowed fields, this should be rejected. if(_.union(ch_feilds, count_feilds).length != count_feilds.length) return false; // Make sure the scores line up if(doc.upc != doc.upvoters.length || doc.downc != doc.downvoters.length || doc.score != doc.upc - doc.downc) return false; break; default: return false; } }); console.log(JSON.stringify({userId: userId, doc: doc, fields: fields, modifier: modifier})); console.log("Update Successful"); return true; }, remove: function (userId, doc) { return false; }, //fetch: ["owner"], transform: function () { //... } }); } </code></pre> <p>Here is the client code</p> <pre><code>function upvote(Comments, comment_id){ var comment = Comments.findOne({_id: comment_id}); console.log(JSON.stringify(comment)); if(comment){ comment.upvoters = _.unique(comment.upvoters.push(Meteor.user()["_id"])); comment.downvoters = _.without(comment.downvoters, Meteor.user()["_id"]); Comments.update( {_id: comment_id}, { $addToSet: {upvoters: Meteor.user()["_id"]}, $pull: {downvoters: Meteor.user()["_id"]}, $set: { upc: comment.upvoters.length, downc: comment.downvoters.length, score: comment.upvoters.length - comment.downvoters.length } } ); } //update_vote_count(comment_id); //console.log(comment_id + ' upvoted'); }; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload