0

I am newbie in Meteor ,I want to use an embedded document in my User collection. I am using simple schema added by collection2 package. But as i define the embedded document as i simply defined in one of my Node.js project and that was running successfully without any errors, but as i defined the same in my Meteor project inside User collection it throws an error i.e

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

My schema is as follows:-

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

As you can see its given an error at location field on which i am using embedded document.

Please tell me how we can achieve the same in Meteor, or i am doing something wrong here. Is there any other schema i need to use rather than simple schema or we can achieve the same via Simple schema as well.

Any help suggestion would be appreciated Thanks

Parveen yadav
  • 2,252
  • 1
  • 21
  • 35

1 Answers1

1

To define embedded validation rules, you need to create a new schema object, and pass it as the field (Simple Schema doesn't look through your object structure) :

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,
    //...
});

And then in your code, you can:

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

You can see another example in the collection2 documentation.

Julien
  • 9,312
  • 10
  • 63
  • 86
  • @ Julien Thanks for you quick reply , but if i define them as a different schema than what is the advantage of embedded document its then something similar to the reference we use in MongoDb and i also need to create a separate file for this schema. Please correct me if i am wrong because this is my first working exp with Meteor, might be i am wrong. – Parveen yadav Jun 05 '16 at 06:58
  • No it won't be stored as a reference. It's just an object that's used internally by simple schema for validation. This is just how you define validation for embedded documents. You don't need to put it in another file, because it's related to the user. Again: it won't make a different collection (like a mysql table), just an embedded field. – Julien Jun 05 '16 at 12:48
  • I've added an example of how you'd insert an object, in case it helps to understand. – Julien Jun 05 '16 at 12:52
  • Thanks for your response now i am understand how to deal with that. Would appreciate if you also let me know how we can store a reference of other collection instead of embedded document in Meteor using simple schema and which is better in which situation ,please provide an example if possible . Because i tried to find out when to use which and which approach is better but not found any solid solution.Thanks in advance – Parveen yadav Jun 05 '16 at 14:13
  • That's a very broad topic. Here's an article about relationships using meteor and mongo: http://meteor.hromnik.com/blog/joins-in-meteorjs-and-mongodb As to when to use which, it depends on how you're going to use your data. Here's a SO question that talks about it: http://stackoverflow.com/questions/5373198/mongodb-relationships-embed-or-reference – Julien Jun 05 '16 at 16:53
  • should i also attach that location schema with users or just simply create a new schema and use that into user schema. But as i used as you suggested via example its again showing me error i.e **Invalid definition for field location** i think there is one or two steps more i need to do or please correct me thanks – Parveen yadav Jun 08 '16 at 13:10