Mark M February 2016

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        

Answers


Stephen Woods February 2016

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
  }
});

Post Status

Asked in February 2016
Viewed 3,247 times
Voted 7
Answered 1 times

Search




Leave an answer