Create a Schema object in Mongoose/Handlebars with custom keys/values

不羁的心 提交于 2019-12-11 00:19:50

问题


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.



来源:https://stackoverflow.com/questions/33891479/create-a-schema-object-in-mongoose-handlebars-with-custom-keys-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!