2

I want to create a form to input custom keys and values of an object in an mongo/mongoose schema to eventually see in a handlebars view. See example to better explain. Any help would be great. :)

Mongoose/Mongodb Schema:

var docketSchema = new Schema({
  staff: [{ String: String, String: String }]
});

Handlebars input view:

<div class="form-group">
    <input value="{{input.staffkey1}}">
    <input value="{{input.staffvalue1}}">
</div>

<div class="form-group">
    <input value="{{input.staffkey2}}">
    <input value="{{input.staffvalue2}}">
</div>
rrudy90023
  • 63
  • 2
  • 8
  • The schema should look like `staff: [{ key: String, value: String }]` and in view you would do {{#each staff}} {{key}}:{{value}}{{/each}} – Molda Nov 24 '15 at 11:05
  • @Molda Thanks. One other question. To target the array from a form input. Is it the following: ?? – rrudy90023 Nov 25 '15 at 06:15
  • Well it depends on what you pass to the view. If you pass {staff: [{key:'key1', val:'val1'}, {key:'key2', val:'val2'}]} then in view you do something like {{#each staff}} {{key}}: {{/each}} – Molda Nov 25 '15 at 06:25
  • @Molda, im using staff : [{ key: string }], my view for my form is {{#each staff}}{{/each}} and does not render my form. Thanks. – rrudy90023 Nov 25 '15 at 06:47
  • How do you pass staff to view? Do you use express, something like res.render('myView', {staff:[{...}]}); – Molda Nov 25 '15 at 06:49
  • @Molda, yes I use express. I have a router do a POST method by using req.body to get form values then push to db. router.post('/create', function(req, res, next) {userService.addDocket(req.body, function(err) { var vm = { input: req.body }; res.redirect('/dockets'); }); }); – rrudy90023 Nov 25 '15 at 06:56
  • And in /dockets page you want to render it, that's where the problem is, right? So show me res.render in router.get('/dockets'... – Molda Nov 25 '15 at 07:15
  • @Molda here it is: router.get('/create', function(req, res, next) { var vm = { title: 'Create a docket', firstName: req.user.firstName }; res.render('dockets/create', vm); }); see clear example here: [link] (http://stackoverflow.com/questions/33907290/target-an-array-in-a-mongo-mongoose-schema-using-a-handlebars-form) – rrudy90023 Nov 25 '15 at 07:26
  • You showing me GET /create but I asked for GET /dockets since I understand that dockets page is where you want to render the `staff` and where you have the problem. So you need to pass staff to render function like vm.staff=[{key:"...", val:".."}] If this isn't it then I'm lost sorry – Molda Nov 25 '15 at 07:43

2 Answers2

0

the reason to use mongoose is usually to ensure that your documents have some known keys and to validate new objects so they conform to your schema.

If you explicitly don't want your objects to have the same keys use the schema-type Mixed - http://mongoosejs.com/docs/schematypes.html:

var docketSchema = new Schema({
   staff: [{}]
});
Reto
  • 3,107
  • 1
  • 21
  • 33
0

You can add strict: false to your schema to add fields to your schema which are not defined.

var docketSchema = new Schema({
    //
}, {strict: false});

Nevertheless it is always better to define your fields.

Thomas Bormans
  • 5,156
  • 6
  • 34
  • 51