Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>function addItem(list_id, item_name) { if(!item_name &amp;&amp; !list_id) return; lists.update({_id:list_id}, {$addToSet:{items:{Name:item_name}}}); } </code></pre> <p>Seems like you're trying to add an object to a set. You're getting an error on simulation. Let's investigate that error. The code that errors out:</p> <p><a href="https://github.com/meteor/meteor/blob/master/packages/minimongo/modify.js">https://github.com/meteor/meteor/blob/master/packages/minimongo/modify.js</a></p> <pre><code> $addToSet: function (target, field, arg) { var x = target[field]; if (x === undefined) target[field] = [arg]; else if (!(x instanceof Array)) throw Error("Cannot apply $addToSet modifier to non-array"); else { ... </code></pre> <p>Uh oh, <code>throw Error("Cannot apply $addToSet modifier to non-array.")</code>.</p> <p>Look at your code:</p> <pre><code>Object Category: "Tools" _id: "eaa681e1-83f2-49f2-a42b-c6d84e526270" ... items: Object ... </code></pre> <p><code>items</code> is an object, not an array! It will error out.</p> <p>Can you <code>$addToSet</code> to an object with Mongo? Let's look at the code.</p> <p><a href="https://github.com/mongodb/mongo/blob/4a4f9b1d6dc79d1ba4a7d7eaa9e4eb6d00aa466c/db/update.cpp">https://github.com/mongodb/mongo/blob/4a4f9b1d6dc79d1ba4a7d7eaa9e4eb6d00aa466c/db/update.cpp</a></p> <pre><code> case ADDTOSET: { uassert( 12592 , "$addToSet can only be applied to an array" , in.type() == Array ); ... } </code></pre> <p>Nope! This is from old Mongo code, because the contemporary codebase is sprawling, but same thing.</p> <p>I only found one <code>insert</code> in your code.</p> <pre><code>'keyup #add-category': function(e, t) { if (e.which === 13) { var catVal = String(e.target.value || ""); if (catVal) { lists.insert({Category:catVal}); Session.set('adding_category', false); } } }, </code></pre> <p>Try <code>lists.insert({Category:catVal,items:[]})</code>. So that items is initialized as an array rather than an object when it was first used.</p> <p>Also, I don't think <code>$addToSet</code> compares objects in an array the way you would like anyway, so consider making a separate collection <code>Items</code> that contains a <code>categoryId</code>.</p> <p>It is purely a coincidence that it is working on one place and not another.</p>
 

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