3

Meteor 的 todos 示例运行良好。但是,当我向 Todos 和 Lists 集合添加架构时,我不断收到“错误:列表 id 必须是一个对象”。任何帮助将不胜感激。

添加了:meteor add aldeed:simple-schema meteor add aldeed:collection2

这是添加到 collections.js 文件中的新架构:

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

其他一切都没有改变。

在我开始流星之前,我做了一个“流星重置”。

这是尝试将新列表的 _id (list_id) 附加到 Todos 架构中的 listId 对象时来自 bootstrap.js 文件的错误:。. . {名称:“Favorite Scientists”,项目:[“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 服务器重新启动 => 启动你的应用程序。

4

1 回答 1

0

Lists.insert()返回新创建的对象的 _id,它是一个字符串,因此您的 list_id 变量返回一个字符串并使您的架构无效。将您的 Schema.Todos listId 类型更改为 String 而不是 Object。

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

Schema.Todos = new SimpleSchema({
  listId: {
    type: String
  },
  text: {
    type: String
  },
  createdAt: {
    type: Date
  }
});
于 2016-02-09T01:12:39.690 回答