Mark M
Mark M

Reputation: 49

Getting Error: List id must be an object after adding schema to todos example in Meteor

The todos example for Meteor works fine. However, when I added a schema to the Todos and Lists collections I keep getting "Error: List id must be an object". Any help would be greatly appreciated.

Added were: meteor add aldeed:simple-schema meteor add aldeed:collection2

Here is the new schema added to the collections.js file:

Lists = new Mongo.Collection('lists');

var Schema = {};

Schema.Lists = new SimpleSchema({
  name: {
    type: String
  },
  incompleteCount: {
    type: Number
  }
});

Lists.attachSchema(Schema.Lists);

Todos = new Mongo.Collection('todos');

Schema.Todos = new SimpleSchema({
  listId: {
    type: Object
  },
  text: {
    type: String
  },
  createdAt: {
    type: Date
  }
});

Todos.attachSchema(Schema.Todos);

Nothing else changed.

Before I started meteor, I did a "meteor reset".

Here is the error from the bootstrap.js file when attempting to attach the _id (list_id) of the new list to the listId object from the Todos schema: . . . {name: "Favorite Scientists", items: ["Ada Lovelace", "Grace Hopper", "Marie Curie", "Carl Friedrich Gauss", "Nikola Tesla", "Claude Shannon" ] } ];

     var timestamp = (new Date()).getTime();
     _.each(data, function(list) {
       var list_id = Lists.insert({name: list.name,
         incompleteCount: list.items.length});

       _.each(list.items, function(text) {     //line 43
         Todos.insert({listId: list_id,        //line 44
                       text: text,
                       createdAt: new Date(timestamp)});
         timestamp += 1; // ensure unique timestamp.
       });
     });

(STDERR)                        throw(ex);
(STDERR)                              ^
(STDERR) Error: List id must be an object
(STDERR)     at getErrorObject (meteor://💻app/packages/aldeed_collection2-core/lib/collection2.js:345:1)
(STDERR)     at [object Object].doValidate (meteor://💻app/packages/aldeed_collection2-core/lib/collection2.js:328:1)
(STDERR)     at [object Object].Mongo.Collection.(anonymous function) [as insert] (meteor://💻app/packages/aldeed_collection2-core/lib/collection2.js:83:1)
(STDERR)     at meteor://💻app/server/bootstrap.js:44:1
(STDERR)     at Array.forEach (native)
(STDERR)     at Function._.each._.forEach (meteor://💻app/packages/underscore/underscore.js:105:1)
(STDERR)     at meteor://💻app/server/bootstrap.js:43:1
(STDERR)     at Array.forEach (native)
(STDERR)     at Function._.each._.forEach (meteor://💻app/packages/underscore/underscore.js:105:1)
(STDERR)     at meteor://💻app/server/bootstrap.js:39:1

=> Meteor server restarted => Started your app.

Upvotes: 3

Views: 285

Answers (1)

Stephen Woods
Stephen Woods

Reputation: 4049

Lists.insert() returns the _id of the newly created object, which is a string, so your list_id variable is returning a string and invalidating your schema. Change your Schema.Todos listId type to String instead of Object.

Todos = new Mongo.Collection('todos');

Schema.Todos = new SimpleSchema({
  listId: {
    type: String
  },
  text: {
    type: String
  },
  createdAt: {
    type: Date
  }
});

Upvotes: 0

Related Questions