Create or update database from array items

This forum contains topics that were moved from Get Satisfaction
Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Create or update database from array items

Post by Yurii Orishchuk »

Hi Mark,

Sure, you can pass there any value you need.

Regards.

Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Create or update database from array items

Post by Mark7294305 »

Hi Yurii
when use that code the results returned is empty. if the value of producty is 6 and the value of stockqty is 5, I want 11 to be displayed

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Create or update database from array items

Post by Yurii Orishchuk »

Hi Mark,

May be problem in string value of your parameters.

Please try to use following JS code:

pre

"Qty_in_Cup": (parseInt(productqty) + parseInt(stockqty))

/pre

Regards.

Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Create or update database from array items

Post by Mark7294305 »

Hi Yurii
That code also failed. It also erases the value which was stored in the database

Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Create or update database from array items

Post by Mark7294305 »

Hi Yurii
Please ignore. The code works ok

Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Create or update database from array items

Post by Mark7294305 »

Hi Yurii
One last question ;-) How do I add a Math.ceil to the stockqty value. So for example

"Qty_in_Cup": (parseInt(productqty) + parseInt(stockqty) return Math.ceil)

Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Create or update database from array items

Post by Mark7294305 »

Hi Yurii
I have also worked out the Math.ceil command. But I have another problem. Some values are received as 0.65 for example and I need to round this value up to 1. If I use the Math.ceil command it returns the value 0. What command can I use to round any value under 1 up to 1?

Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Create or update database from array items

Post by Mark7294305 »

Hi Yurii
Please ignore. All working using a if statement ;-))

Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Create or update database from array items

Post by Mark7294305 »

Hi Yurii
I still have a slight problem with this. Here is my current server code.


var DB_id = "54ae8e1ce4b042777ffe572c";
var collectionName = "CupboardStore_Coll";
var productid = request.get("productid");
var newproductname = request.get("newproductname");
var productbarcode = request.get("productbarcode");
var deptName = request.get("deptName");
var shelfid = request.get("shelfid");

var productqty = request.get("ProductQty");
var itemName = request.get("itemName");
var Product_Desc = request.get("Product_Desc");

if (productqty < 1)

Code: Select all

{ productqty = 1 

}; 

var query = [] ;

try

{

// result = {};
var result = [];
var params = {};
params.criteria =

{

Code: Select all

//Barcode: productbarcode 
 ASDAcin: productbarcode 

};

Code: Select all

 query = Collection.query(DB_id,collectionName,params) ;  

if (query.length === 0)

Code: Select all

{ 

        Collection.createObject(DB_id, collectionName, { 

          "Barcode": productbarcode, "Product": newproductname, "Qty_in_Cup": productqty, "Qty_Max": (parseInt(productqty) + 1), "Consumed": 1, "Restock_Level": 0, "Restock_Flag": productqty, "Restock_Qty": 1, "ASDAcin": productbarcode, "deptName": deptName, "shelfid": shelfid, "Substitutions": false 
         }); 

} else{

Code: Select all

 for (var i=0; i < query.length; i++){ 
   var currentObject = query[i]; 

         Collection.updateObject(DB_id, collectionName, currentObject["_id"], { 

"Qty_in_Cup": parseInt(currentObject["Qty_in_Cup"]) + (parseInt(productqty))
, "Consumed": parseInt(currentObject["Qty_Max"]) - (parseInt(currentObject["Qty_in_Cup"]) + (parseInt(productqty))), "Restock_Qty": parseInt(currentObject["Qty_Max"]) - (parseInt(currentObject["Qty_in_Cup"]) + (parseInt(productqty))), "Restock_Flag": (parseInt(currentObject["Restock_Flag"]) + (parseInt(productqty))),
"deptName": deptName, "shelfid": shelfid, "Product_Desc": Product_Desc, "CrossOff_SL": false

Code: Select all

     }); 

 } 

 response.success(result); 

}

} catch (e)

{

Code: Select all

 response.success("message: " + e.message + "ncode: " + e.code); 

}


When I upload the first set of products all works ok.

Image

when re upload the first set of products. The "Qty_in_Cup" Column in the database updates as required

Image

However If I add a new set of products. The columns in the database are emptied.

Image

How Can I fix this?

Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Create or update database from array items

Post by Mark7294305 »

Can anyone else on the support team help me with this??

Post Reply