我想使用json在数据库中插入表单值。我的html代码如下。
<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格式的值,如下所示
{"Active Template Id":"6467","Shirt Brand":"levis","ProductId":"EB301","Brand":"on","Material":"cotton","Price":"1800","Combo Id":"S90"}
得到这个我在html里面的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值存储在&#34;产品&#34;采集。 在我的meteor项目中,js文件就是这个
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");
},
};
}
答案 0 :(得分:1)
从技术上讲,您可以按原样插入json,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);
}
但正如我在上面链接的答案中所述,最好只使用小写字母字符和下划线来获得更多正式的属性名称。