如何在Meteor中使用嵌入式文档

时间:2016-06-04 18:19:37

标签: javascript node.js mongodb meteor simple-schema

我是Meteor的新手,我想在我的用户集中使用嵌入式文档。我使用collection2包添加的简单模式。但是当我定义嵌入式文档时,我只是在我的一个Node.js项目中定义并且运行成功而没有任何错误,但正如我在Meteor项目中定义的那样在User集合中它会抛出错误,即< /强>

    /home/parveen/.meteor/packages/meteor-tool/.1.3.2_4.10vjklo++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:267
W20160604-23:22:34.819(5.5)? (STDERR)                       throw(ex);
W20160604-23:22:34.819(5.5)? (STDERR)                             ^
W20160604-23:22:34.941(5.5)? (STDERR) Error: Invalid definition for location field.
W20160604-23:22:34.941(5.5)? (STDERR)     at packages/aldeed_simple-schema/simple-schema.js:457:1
W20160604-23:22:34.941(5.5)? (STDERR)     at Function._.each._.forEach (packages/underscore/underscore.js:113:1)
W20160604-23:22:34.941(5.5)? (STDERR)     at [object Object].SimpleSchema (packages/aldeed_simple-schema/simple-schema.js:454:1)
W20160604-23:22:34.941(5.5)? (STDERR)     at meteorInstall.collections.Users.js (collections/Users.js:11:14)
W20160604-23:22:34.942(5.5)? (STDERR)     at fileEvaluate (packages/modules-runtime/.npm/package/node_modules/install/install.js:141:1)
W20160604-23:22:34.942(5.5)? (STDERR)     at require (packages/modules-runtime/.npm/package/node_modules/install/install.js:75:1)
W20160604-23:22:34.942(5.5)? (STDERR)     at /home/parveen/differentialImaging/.meteor/local/build/programs/server/app/app.js:957:1
W20160604-23:22:34.942(5.5)? (STDERR)     at /home/parveen/differentialImaging/.meteor/local/build/programs/server/boot.js:283:10
W20160604-23:22:34.942(5.5)? (STDERR)     at Array.forEach (native)
W20160604-23:22:34.942(5.5)? (STDERR)     at Function._.each._.forEach (/home/parveen/.meteor/packages/meteor-tool/.1.3.2_4.10vjklo++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
=> Exited with code: 8
=> Your application is crashing. Waiting for file change.

我的架构如下: -

 UserSchema = new SimpleSchema({
name:{
    type: String,
    trim: true,
    optional: true
},
email:{
    type:String,
    trim: true,
    optional: true
},
password:{
    type:String,
    trim: true,
    optional: true
},
location: {
         latitude: {
             type: Number,
             default: 0,
             required: false
         },
         longitude: {
             type: Number,
             default: 0,
             required: false
         },
         state: {
             type: String,
             default: '',
             requried: false,
             trim: true
         },
         city: {
             type: String,
             default: '',
             requried: false,
             trim: true
         }
     },
createdAt:{
    type:Date,
    label:"Created At",
    autoValue:function(){
        return new Date();
    }

}
});
Users.attachSchema(UserSchema);

正如您所看到的,我在使用嵌入式文档的位置字段中给出了错误。

请告诉我如何在Meteor中实现同样的目标,或者我在这里做错了什么。是否需要使用其他模式而不是简单模式,或者我们也可以通过简单模式实现相同的模式。

任何帮助建议将不胜感激 感谢

1 个答案:

答案 0 :(得分:1)

要定义嵌入式验证规则,您需要创建一个新的架构对象,并将其作为字段传递(Simple Schema不会查看您的对象结构):

LocationSchema = new SimpleSchema({
         latitude: {
             type: Number,
             default: 0,
             required: false
         },
         longitude: {
             type: Number,
             default: 0,
             required: false
         },
         state: {
             type: String,
             default: '',
             requried: false,
             trim: true
         },
         city: {
             type: String,
             default: '',
             requried: false,
             trim: true
         }
});

UserSchema = new SimpleSchema({
    //... other fields
    location: LocationSchema,
    //...
});

然后在您的代码中,您可以:

Users.insert({
  //...
  location: {latitude: 4.12, longitude: 6.18, state:"Colorado", city "Whatever"}
  //...
});

您可以在the collection2 documentation中看到另一个示例。