Create a Schema object in Mongoose/Handlebars with

2019-07-17 22:13发布

问题:

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>

回答1:

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: [{}]
});


回答2:

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.