How to use Embedded document in meteor using SimpleSchema

Posted By : Parveen Kumar Yadav | 14-Jul-2016

If you are familiar with node you are well aware with Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment.. So when you define your schema for any collection than if you want to embed any object into it than it should looks like:-

UserSchema = new mongooseSchema({
name:{
    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();
    }
}
});
 

In this we embed location as diff object so if you want to achieve same in Meteor it will throw an error:-

If you want to make schema in Meteor than you can use a package name simpleSchema add as follows:-

 meteor add aldeed:simple-schema
 

After this you need to define schema like:-

UserSchema = new SimpleSchema({
name:{
    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);
 

In Meteor it will throw an error:-

/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.
 

So if you want to embed any document in meteor you need to create a separate schema for that like:-

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:{
	type:LocationSchema
},
    //...
});
 

In this way you can embed the document into meteor.

Here is the stack-overflow link for my question:-

http://stackoverflow.com/questions/37633584/how-to-use-embedded-document-in-meteor

Thanks!

About Author

Author Image
Parveen Kumar Yadav

Parveen is an experienced Java Developer working on Java, J2EE, Spring, Hibernate, Grails ,Node.js,Meteor,Blaze, Neo4j, MongoDB, Wowza Streaming Server,FFMPEG,Video transcoding,Amazon web services, AngularJs, javascript. He likes to learn new technologies

Request for Proposal

Name is required

Comment is required

Sending message..