Javascript 使用meteor在数据库中插入表单值(json格式)

Javascript 使用meteor在数据库中插入表单值(json格式),javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我想使用json在数据库中插入表单值 <template name="dpVar"> <h1>variants</h1> <form id="form" autocomplete="off" action="" method="post"> <table class="table table-responsive table-bordered table-hover"> &

我想使用json在数据库中插入表单值

    <template name="dpVar">
      <h1>variants</h1>
      <form id="form" autocomplete="off" action="" method="post"> 

      <table class="table table-responsive table-bordered table-hover">
        <tbody>
         {{#each variant}}
         {{#each VARIENTS}}
          {{#if $eq this.DATATYPE "Text"}}
          <tr>
           <td class="center">{{this.NAME}}</td>
           <td>
            <input type="text" name={{this.NAME}}>
           </td> 

           </tr>
          {{/if}}

           {{#if $eq this.DATATYPE "price"}}
            <tr>
            <td class="center">{{this.NAME}}</td>
            <td><input type="text" name={{this.NAME}}></td> 
           </tr>
          {{/if}}

        {{#if $eq this.DATATYPE "radio"}}
         <tr>
          <td class="center">{{this.NAME}}</td>
         <td>
         <input type="radio" name={{this.NAME}}>

        </td> 
        </tr>
       {{/if}}
        {{#if $eq this.DATATYPE "string"}}
         <tr>
          <td class="center">{{this.NAME}}</td>
          <td><input type="text" name={{this.NAME}}></td> 
         </tr>
        {{/if}}

        {{/each}}
        {{/each}}
        </tbody>
       </table>


      <input type="submit" value="create new product" id="submit"    class="btn btn-success addproduct">  

   </form>
  <h2>JSON</h2>
  <pre id="json">
  </pre>
 </template>
为了实现这一点,我在下面的head中使用了html中的javascript代码

    <script type="text/javascript">

       $.fn.serializeObject = function()
       {
      var o = {};
       var a = this.serializeArray();
      $.each(a, function() {
      if (o[this.name] !== undefined) {
         if (!o[this.name].push) {
            o[this.name] = [o[this.name]];
        }
         o[this.name].push(this.value || '');
       } else {
        o[this.name] = this.value || '';
      }
    });
   return o;
 };

       $(function() {
         $('form').submit(function() {
       $('#json').text(JSON.stringify($('form').serializeObject()));
       return false;
      });
   }); 
   </script> 

从技术上讲,您可以按原样插入json:

但正如我在上面链接的答案中所述,最好使用更正式的属性名称,只使用小写字母和下划线

    <script type="text/javascript">

       $.fn.serializeObject = function()
       {
      var o = {};
       var a = this.serializeArray();
      $.each(a, function() {
      if (o[this.name] !== undefined) {
         if (!o[this.name].push) {
            o[this.name] = [o[this.name]];
        }
         o[this.name].push(this.value || '');
       } else {
        o[this.name] = this.value || '';
      }
    });
   return o;
 };

       $(function() {
         $('form').submit(function() {
       $('#json').text(JSON.stringify($('form').serializeObject()));
       return false;
      });
   }); 
   </script> 
    var nodeDB = new Meteor.Collection('nodes');
    var productDB = new Meteor.Collection('products');

    if (Meteor.isClient){
     Template.DBelements.nodes = nodeDB.find();
     Template.dpVar.variant=nodeDB.find({"ACTIVE" : 1, "VARIENTS.ACCESS" : "PUBLIC"}, { "VARIENTS.NAME": 1,"VARIENTS.DATATYPE":1, _id : 0 } );
   Template.ActiveTemplateDetails.nodestemp=nodeDB.find({"ACTIVE" : 1},{ _id : 0});
   result=[];
  Meteor.call('getApiResult', function (err, res) {
    if (res) {
        console.log("reached meteor call")
        console.log(res);
        result=res;

        }
   });

   Template.DBelements.events = {
   'click .remove': function () {
    nodeDB.remove(this._id);
    console.log(this._id, "removed from the database");
   },

 'submit .addPVForm' : function (e) {
   e.preventDefault();
   nodeDB.update(this._id,{$push:{subject: $(".subject").val()}});
 },
 'click .active' : function  (err) {
     console.log("current id is ",this._id);

     activeTemplateID=this._id;
  //   forEach (nodes) {
   //  if(this._id){nodeDB.update(this._id, {$set:{'ACTIVE':1}});}
    // else{nodeDB.update({_id: { $ne: activeTemplateID}}, { $set: { 'ACTIVE':0 } },{multi:true} );  }   
    //}
   nodeDB.find().forEach(function(nodes) {
   nodeDB.update({_id:nodes._id},{$set:{ 'ACTIVE':0 }});
    });
   nodeDB.update({_id:activeTemplateID},{$set:{ 'ACTIVE':1 }});
 }
};


   Template.dpVar.events = {
    'click .addproduct': function () {
    // var prd=$("#json");
    //alert(prd);

      // alert("hello");

    },

    };

 }
'click .addproduct': function () {
  var prd=JSON.parse($("#json").text());
  productDB.insert(prd); 
}