使用meteor在数据库中插入表单值(json格式) [英] Insert form value (json format) in database using meteor

查看:49
本文介绍了使用meteor在数据库中插入表单值(json格式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用json在数据库中插入表单值。我的html代码如下。

i want to insert form values in database using json.My html code is below.

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

当我在文本框中填充值后点击按钮我得到json格式的值就像这样

when i click on button after filling values in textboxes i am getting the values in json format in like this

    {"Active Template Id":"6467","Shirt Brand":"levis","ProductId":"EB301","Brand":"on","Material":"cotton","Price":"1800","Combo Id":"S90"}

为了得到这个我在html里面使用javascript代码在头下面

to get this i am using a javascript code in html inside head that is below

    <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值存储在products集合的数据库中。
在我的meteor项目中,js文件是这个

I want to store this json value in database in " products " collection. In my meteor project the js file is this

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

    },

    };

 }


推荐答案

技术上你可以按原样插入你的json,只要密钥不包含点或以美元符号开头: / p>

Technically you could just insert your json as-is, as long as keys don't contain dots or start with a dollar sign :

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

但正如我上面提到的答案所述,最好是更正式的属性名称,仅使用小写字母和下划线。

But as stated in the answer I linked above, it would be better to have more formal property names using only lowercase alphabetic characters and underscores.

这篇关于使用meteor在数据库中插入表单值(json格式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆