I am following the recently published book “Getting Started with Meteor.js JavaScript Framework” by Isaac Strack. The book works with Meteor 0.5.0. I am working with version 0.5.4.
In the book you build an app with a few categories to which you insert data for tracking household items, and who they may be lent to. I deployed the app to a meteor subdomain, and it is working perfectly. It does not replicate my local MongoDB error.

I am in Chapter 5, and I have just removed autopublish from the app, and specified my local channels for data.
Locally, under the under the “Tools” category only, when I try to add a new item to the category, I recieve this error in my browser console:
Exception while simulating the effect of invoking '/Lists/update' Error {} Error: Cannot apply $addToSet modifier to non-array
at Error (<anonymous>)
at LocalCollection._modifiers.$addToSet (http://localhost:3000/packages/minimongo/modify.js?e7f02f0df0bff9f0b97236f9548637b7ede1ac74:178:13)
at Function.LocalCollection._modify (http://localhost:3000/packages/minimongo/modify.js?e7f02f0df0bff9f0b97236f9548637b7ede1ac74:53:9)
at LocalCollection._modifyAndNotify (http://localhost:3000/packages/minimongo/minimongo.js?7f5131f0f3d86c8269a6e6db0e2467e28eff6422:474:19)
at LocalCollection.update (http://localhost:3000/packages/minimongo/minimongo.js?7f5131f0f3d86c8269a6e6db0e2467e28eff6422:444:12)
at m.(anonymous function) (http://localhost:3000/packages/mongo-livedata/collection.js?3ef9efcb8726ddf54f58384b2d8f226aaec8fd53:415:36)
at http://localhost:3000/packages/livedata/livedata_connection.js?367884963b120d457819216ff713b2586b266dde:540:25
at _.extend.withValue (http://localhost:3000/packages/meteor/dynamics_browser.js?46b8d1f1158040fcc2beb7906ec2f932871a398d:21:19)
at _.extend.apply (http://localhost:3000/packages/livedata/livedata_connection.js?367884963b120d457819216ff713b2586b266dde:539:47)
at Meteor.Collection.(anonymous function) [as update] (http://localhost:3000/packages/mongo-livedata/collection.js?3ef9efcb8726ddf54f58384b2d8f226aaec8fd53:266:23) logging.js:30
update failed: Internal server error logging.js:30
The tools category already has one item in it which was submitted earlier in the tutorial. If I type into the console lists.findOne({Category:"Tools"}); I get the output which recognizes an item in the Object:
Object
Category: "Tools"
_id: "eaa681e1-83f2-49f2-a42b-c6d84e526270"
items: Object
LentTo: "Steve"
Name: "Linear Compression Wrench"
Owner: "me"
__proto__: Object
__proto__: Object
However, the screen output is blank:

Naturally I have tried restarting the meteor server & shut down the browser, but no resolution. I am new to MongoDB, so I am unclear as to where to turn to understand what is causing this problem, or why.
You can view the app here. You can view the code on my GitHub.
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:
https://github.com/meteor/meteor/blob/master/packages/minimongo/modify.js
Uh oh,
throw Error("Cannot apply $addToSet modifier to non-array.").Look at your code:
itemsis an object, not an array! It will error out.Can you
$addToSetto an object with Mongo? Let’s look at the code.https://github.com/mongodb/mongo/blob/4a4f9b1d6dc79d1ba4a7d7eaa9e4eb6d00aa466c/db/update.cpp
Nope! This is from old Mongo code, because the contemporary codebase is sprawling, but same thing.
I only found one
insertin your code.Try
lists.insert({Category:catVal,items:[]}). So that items is initialized as an array rather than an object when it was first used.Also, I don’t think
$addToSetcompares objects in an array the way you would like anyway, so consider making a separate collectionItemsthat contains acategoryId.It is purely a coincidence that it is working on one place and not another.