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

Create or update database from array items

Hello Support
My concerns are if I was to purchase the Advisory Pack, would you also take 25 days to come to this answer as you did above?

I have made some progression with the server code

I was using the following code with the query parameter which was working ok but was restricted by 18 objects

-----------------

var DB_id = "xxxxxxx";
var collectionName = "xxxx";
var cin = [];
var query = [];
var data = [];
var result = [];

cinquery = request.get("ProductsUpload");

data = JSON.parse(cinquery);

for (var i = 0; i < data.length; i++) {

cin = (data.cin);
name = (data.name);
qty = (data.qty);
deptName = (data.deptName);
shelfid = (data.shelfId);
itemName = (data.itemName);
Product_Desc = (data.Product_Desc);
weight = (data.weight);
details = (parseInt(cin));
result.push(data.cin, data.name, data[i].qty, data[i].shelfId, data[i].deptName, data[i].itemName, data[i].Product_Desc, data[i].weight);

try

{
var params = {};

params.criteria ={
Barcode: cin
};

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

if (query.length === 0)

{

Collection.createObject(DB_id, collectionName, {
"Barcode": data[i].cin,
"UPCBarcode": data[i].cin,
"Product": data[i].name,
"Qty_in_Cup": (parseInt(data[i].qty)),
"Qty_Max": (parseInt(data[i].qty)),
"Consumed": 0,
"Restock_Level": 0,
"Restock_Flag": (parseInt(data[i].qty)),
"Restock_Qty": 0,
"ASDAcin": data[i].cin,
"deptName": data[i].deptName,
"shelfid": data[i].shelfId,
"CrossOff_SL": false,
"itemName": data[i].itemName,
"Product_Desc": data[i].Product_Desc,
"InvisableinCSList": false,
"Substitutions": false,
"Size_Weight": data[i].weight
});
} else {

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

Collection.updateObject(DB_id, collectionName, currentObject["_id"], {
"Qty_in_Cup": (parseInt(currentObject["Qty_in_Cup"]) + parseInt(qty)),
"Consumed": parseInt(currentObject["Qty_Max"]) - (parseInt(currentObject["Qty_in_Cup"]) + (parseInt(qty))),
"Restock_Qty": parseInt(currentObject["Qty_Max"]) - (parseInt(currentObject["Qty_in_Cup"]) + (parseInt(qty))),
"Restock_Flag": (parseInt(currentObject["Restock_Flag"]) + (parseInt(qty))),
"deptName": deptName,
"shelfid": shelfid,
"itemName": itemName,
"Product_Desc": Product_Desc,
"InvisableinCSList": false,
"CrossOff_SL": false,
"Substitutions": false
});

}}
console.log(cin)
response.success(result);

} catch (e)

{

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

---------

I now need to change the code to use the response BODY. I tried the following but it has failed

---------

var DB_id = "xxxxxxx";
var collectionName = "xxxx";
var cin = [];
var query = [];
var data = [];
var result = [];
var responseBody = {},
requestParams = {},
paramKeys = request.keys();

for (var key = 0; key < paramKeys.length; key++) {
requestParams[paramKeys[key]] = request.get(paramKeys[key]);
responseBody.requestBody = request.body();
responseBody.requestParams = requestParams;

//data = JSON.parse(requestParams[paramKeys[key]]); //this is the old line which works with the query parameter
data = JSON.parse(responseBody.requestBody); //this is the new line which fails
for (var i = 0; i < data.length; i++) {

cin = (data.cin);
name = (data.name);
qty = (data.qty);
deptName = (data.deptName);
shelfid = (data.shelfId);
itemName = (data.itemName);
Product_Desc = (data.Product_Desc);
weight = (data.weight);
details = (parseInt(cin));
result.push(data.cin, data.name, data[i].qty, data[i].shelfId, data[i].deptName, data[i].itemName, data[i].Product_Desc, data[i].weight);

try

{
var params = {};

params.criteria ={
Barcode: cin
};

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

if (query.length === 0)

{

Collection.createObject(DB_id, collectionName, {
"Barcode": data[i].cin,
"UPCBarcode": data[i].cin,
"Product": data[i].name,
"Qty_in_Cup": (parseInt(data[i].qty)),
"Qty_Max": (parseInt(data[i].qty)),
"Consumed": 0,
"Restock_Level": 0,
"Restock_Flag": (parseInt(data[i].qty)),
"Restock_Qty": 0,
"ASDAcin": data[i].cin,
"deptName": data[i].deptName,
"shelfid": data[i].shelfId,
"CrossOff_SL": false,
"itemName": data[i].itemName,
"Product_Desc": data[i].Product_Desc,
"InvisableinCSList": false,
"Substitutions": false,
"Size_Weight": data[i].weight
});
} else {

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

Collection.updateObject(DB_id, collectionName, currentObject["_id"], {
"Qty_in_Cup": (parseInt(currentObject["Qty_in_Cup"]) + parseInt(qty)),
"Consumed": parseInt(currentObject["Qty_Max"]) - (parseInt(currentObject["Qty_in_Cup"]) + (parseInt(qty))),
"Restock_Qty": parseInt(currentObject["Qty_Max"]) - (parseInt(currentObject["Qty_in_Cup"]) + (parseInt(qty))),
"Restock_Flag": (parseInt(currentObject["Restock_Flag"]) + (parseInt(qty))),
"deptName": deptName,
"shelfid": shelfid,
"itemName": itemName,
"Product_Desc": Product_Desc,
"InvisableinCSList": false,
"CrossOff_SL": false,
"Substitutions": false
});

}}
console.log(cin)
response.success(result);

} catch (e)

{

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

----

This line which is failing is
data = JSON.parse(responseBody.requestBody);
can you help me correct this code?

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Create or update database from array items

Hello Mark,

There may be some delays with answers for Advisory pack due to weekends or complicated questions, but most often a developer starts to work on an issue as soon as he gets it.

Amr Osman
Posts: 0
Joined: Sat Nov 14, 2015 11:58 am

Create or update database from array items

I want to read an array value passed to server code. Is my code is right
lets Say my _IDS is an array storage in my app i.e IDS

Code: Select all

 
 var requestedArray = []; 
 requestedArray = request.get("_IDS"); 

 for (var i = 0;i< requestedArray.length; i++){ 
 //Do Iteration here 
 } 
 [code]
Amr Osman
Posts: 0
Joined: Sat Nov 14, 2015 11:58 am

Create or update database from array items

your code is incorrect in the Update Section you should use
code query[key]&#46;id or query[key]&#46;Qty_in_cup /code
instead of
codecurrentObject["id"] /code
and So on.
No need to pass
code var currentObject = query[key]; /code

Return to “Issues”