【问题标题】:Getting Error: List id must be an object after adding schema to todos example in Meteor出现错误:在 Meteor 中将模式添加到 todos 示例后,列表 id 必须是一个对象
【发布时间】:2016-05-18 20:32:06
【问题描述】:

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

添加了: 流星添加 aldeed:simple-schema 流星添加 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 文件中的错误: . . . {名称:“最喜欢的科学家”, 项目:[“Ada Lovelace”, “格蕾丝·霍珀”, “玛丽居里”, “卡尔·弗里德里希·高斯”, “尼古拉·特斯拉”, 《克劳德·香农》 ] } ];

     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 服务器重启 => 启动你的应用程序。

【问题讨论】:

    标签: mongodb meteor simple-schema meteor-collection2


    【解决方案1】:

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

    【讨论】:

    • 谢谢斯蒂芬,就是这样!感谢您的帮助和您的快速回复。
    猜你喜欢
    • 2017-05-02
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 2014-12-13
    相关资源
    最近更新 更多