[英]how to insert multiple data fields in one string in mongodb nodejs
我正在使用node.js進行發票項目,其中我必須在字符串中存儲多個產品,而我的代碼是
架構文件
var mongoose = require('mongoose');
var autoIncrement = require('mongoose-auto-increment');
var invoiceSchema = mongoose.Schema({
date: String,
documentNo:Number,
vendorName:String,
pno:Number,
productName:String,
description:String,
quantity:Number,
costPrice :Number,
mrp:Number,
invoiceNo:Number,
advance:Number,
due:Number,
discount:Number,
Tax : String,
netTotal:Number,
size:String,
colour:String,
grandTotal:Number
},{collection:'invoice-data'});
var invoice = module.exports = mongoose.model('invoice',invoiceSchema);
現在用於路由
var product = require('../models/product');
var invoice= require('../models/invoice.js');
exports.addaInvoice=function(req, res) {
var date=req.body.date;
var invoiceNo=req.body.invoiceNo;
var vendorName = req.body.vname;
var pno = req.body.orderNo;
var productName = getElementById(req.body.productname);
var description = req.body.desc;
var colour = req.body.colour;
var size = req.body.size;
var quantity = req.body.quantity;
var costPrice = req.body.costPrice;
var mrp = req.body.mrp;
var amount=req.body.amount;
var discount=req.body.discount;
var discountvalue=req.body.discountvalue;
var netamount=req.body.netamount;
var advance = req.body.advance;
var due = req.body.due;
var Tax = req.body.Tax;
var netTotal = req.body.netTotal;
var subTotal = req.body.subTotal;
var grandTotal = req.body.grandTotal;
var newInvoice = new invoice({
date: date,
invoiceNo:invoiceNo,
vendorName:vendorName,
pno:pno,
productName:productName,
description:description,
colour:colour,
size:size,
quantity:quantity,
costPrice :costPrice,
amount:amount,
discount:discount,
discountvalue:discountvalue,
netamount:netamount,
mrp:mrp,
advance:advance,
due:due,
Tax :Tax,
netTotal:netTotal,
subTotal : subTotal,
grandTotal : grandTotal
});
newInvoice.save();
req.flash('success_msg',' purchase invoice added ')
res.redirect('/users/invoicedetails');
}
HTML代碼
<head>
<script type="text/javascript">
function calculate()
{
var quantity = document.getElementById('quantity');
var costprice = document.getElementById('costprice');
//var amount = document.getElementById('amount');
var result=quantity.VA*costprice.value;
//amount.value = result;
$("#amount").val(result);
}</script>
<SCRIPT language="javascript">
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
//alert(newcell.childNodes);
switch(newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value = "";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
newcell.childNodes[0].selectedIndex = 0;
break;
}
}
}
</SCRIPT>
</head>
<div class="form-group">
<label class="col-md-4 control-label" for="date">date</label>
<div class="col-md-8">
<input id="date" name="date" placeholder="date" class="form-control input-md" type="date">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="invoiceNo">invoiceNo</label>
<div class="col-md-8">
<input id="invoiceNo" name="invoiceNo" placeholder="invoiceNo" class="form-control input-md" type="text">
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="vendorname">Vendor name</label>
<div class="col-md-8">
<select id="vendorName" name="vendorName" class="form-control">
{{# each items }}
<option> {{ this.vendorName}}</option>
{{/each}}
</select>
</div>
</div>
</td>
</tr>
</table>
<INPUT type="button" value="Add Product " onclick="addRow('dataTable')" />
<table class="table table-hover" >
<thead>
<tr>
<td>productno</td>
<td>productName</td>
<td>size</td>
<td>colour</td>
<td>quantity</td>
<td>costprice</td>
<td>MRP</td>
<td>amount</td>
<td>discount%</td>
<td>discount value</td>
<td>net amount</td>
</tr>
</thead>
<TABLE id="dataTable" class="table table-hover" >
<TR>
<td>
<input id="orderNo" name="orderNo" placeholder="productno" class="form-control input-md" required="" type="text">
</td>
{{# each items }}
<option value="{{ this.productid}}"> {{ this.productname}}</option>
{{/each}}
</select>
</td>
<td>
<select id="size" name="size" class="form-control-md">
{{# each items }}
<option > {{ this.size}}</option>
{{/each}}
</select>
<!-- Text input-->
</td>
<td>
<select id="colour" name="colour" class="form-control-md">
{{# each items }}
<option > {{ this.colour}}</option>
{{/each}}
</select>
</td>
<td>
<input id="quantity" name="quantity" placeholder="Quantity" class="form-control " type="text">
</td>
<td>
<input id="costPrice" name="costPrice" placeholder="costPrice" class="form-control" type="text" onChange="calculate()">
</td>
<!-- Text input-->
<td>
<input id="mrp" name="mrp" placeholder="mrp" class="form-control" type="text">
</td>
<td>
<td>
<input id="amount" name="amount" placeholder="amount" class="form-control" >
</td>
<td>
<input id="discount" name="discount" placeholder="discount%" class="form-control"type="text">
</td>
<td>
<input id="discountvalue" name="discountvalue" placeholder="discountvalue" class="form-control"type="text">
</td>
<td>
<input id="netamount" name="netamount" placeholder="netamount" class="form-control"type="text">
</td>
</tr>
</tbody>
</table>
<table>
<!-- Text input-->
<td>
<label class="col-md-8 control-label" for="advance">Advance</label>
<div class="col-md-4">
<input id="advance" name="advance" placeholder="Advance" class="form-control input-md" type="text">
</div>
<div class="form-group">
<label class="col-md-8 control-label" for="Tax">Tax</label>
<div class="col-md-4">
<select id="Tax" name="Tax" class="form-control">
<option value="14.50">14.10</option>
<option value="12.50">12.50</option>
</select>
</div>
</div>
</td>
</table>
</table>
<label class="col-md-8 control-label" for="netTotal">netTotal</label>
<div class="col-md-4">
<input id="netTotal" name="netTotal" placeholder="netTotal" class="form-control input-md" type="text">
</div>
<label class="col-md-8 control-label" for="subTotal">SubTotal</label>
<div class="col-md-4">
<input id="subTotal" name="subTotal" placeholder="subTotal" class="form-control input-md" type="text">
<label class="col-md-8 control-label" for="grandTotal">grandTotal</label>
<div class="col-md-4">
<input id="grandTotal" name="grandTotal" placeholder="grandTotal" class="form-control input-md" type="text">
<label class="col-md-8 control-label" for="due">due</label>
<div class="col-md-4" align="left">
<input id="due" name="due" placeholder="due" class="form-control input-md" type="text">
</div>
<!-- Button -->
<div class="form-group" align="bottom">
<label class="col-md-4 control-label" for="vadd"></label>
<div class="col-md-4">
<button id="iadd" name="iadd" class="btn btn-primary">ADD</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
這是我的html代碼,腳本用於克隆表格,用於在tablle中添加一行以進行多個產品輸入
現在,如果我有2個或更多產品,這將適用於一種產品
如何將這三種產品保存在與購買發票相同的文檔中
如果您使用的是bodyParser中間件,則可以從客戶端傳遞值數組。
在客戶端,您可能需要按如下方式命名輸入元素
<input name="products[#{1}][productname]" type="text">
<input name="products[#{1}][description]" type="text">
<input name="products[#{2}][productname]" type="text">
<input name="products[#{2}][description]" type="text">
等等
這樣你的請求體就會有
{
"products": {
"1": {
"product name": "name goes here ",
"description": "desc goes here "
},
"2": {
"product name": "name goes here",
"description": "desc goes here"
}
}
}
您可以迭代並獲取路由器中的值
var products = Array.prototype.slice.call(request.body.products);
for (var i = 0; i < messages.length; i++) {
var newInvoice = new invoice({
productName: products[i].productname,
description: products[i].description
});
newInvoice.save();
});
首先,您需要更改發票架構,以在其中添加多個產品。 您需要添加一系列產品子文檔,我在下面添加了一個示例模式
// Product Schema
var ProductSchema = mongoose.Schema({
pno:Number,
productName:String,
description:String,
quantity:Number,
costPrice :Number,
mrp:Number,
size:String,
colour:String,
});
var invoiceSchema = mongoose.Schema({
date: String,
documentNo:Number,
vendorName:String,
products: [ProductSchema],
invoiceNo:Number,
advance:Number,
due:Number,
discount:Number,
Tax : String,
netTotal:Number,
grandTotal:Number
},{collection:'invoice-data'});
我沒有在語法上檢查這段代碼,如果你想在一個文檔中存儲多個子文檔,我只是想知道如何創建模式。
在您的node.js代碼中,您還需要保存一系列產品
var productArr = [];
var temProduct = {};
temProduct.pno= pno;
temProduct.productName= productName;
temProduct.description= description;
temProduct.colour= colour;
temProduct.size= size;
temProduct.quantity= quantity;
temProduct.costPrice = costPrice;
temProduct.amount = amount;
temProduct. = mrp;
productArr.push(temProduct);
var newInvoice = new invoice({
date: date,
invoiceNo:invoiceNo,
vendorName:vendorName,
products:productArr,
advance:advance,
due:due,
Tax :Tax,
netTotal:netTotal,
subTotal : subTotal,
grandTotal : grandTotal
});
newInvoice.save(function(err, new_data) {
if(err) {
// Handle error here, if any come
} else {
req.flash('success_msg',' purchase invoice added ')
res.redirect('/users/invoicedetails');
}
});
您可以像在productArr中一樣推送產品,然后將其添加到發票對象中,然后保存。
我再次提到,上面的代碼不好直接運行,你需要做一些改動,但它會讓你知道如何做到這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.