Create or update database from array items
Just to give you an update on this. The code fails when processing 1 to 3 items but seems to work when processing 4 or more items
Catch up wih the Appery.io community on our forum. Here you'll find information on the lastest questions and issues Appery.io developers are discussing.
https://forum.appery.io/
Just to give you an update on this. The code fails when processing 1 to 3 items but seems to work when processing 4 or more items
Hi Mark,
It seems you pass "null" or "undefined" value to the consumed field.
Please add following log before "update" code to see in trace what there is:
precode
console.log("you pass this value to "Consumed" field");
console.log(parseInt(currentObject["Qty_Max"]));
/code/pre
Regards
Hi Yurii
It is very strange but the problem seems to have auto corrected itself
However I have one last problem with this. When uploading the products not all products are uploading. For example if there 20 items to be uploaded 16 are uploaded and 4 are not. When I press F12 I see 429 (Too Many Requests) and
400 (Bad Request) errors against the items that were not uploaded. I can I fix this?
Hi Mark,
Please show us these errors screen shots.
Thanks.
Hi Mark,
I can see a lot of requests.. You should have single request with all items in request parameters to update.
How do you invoke service to update/create items?
Show us more info.
Regards.
Hi Yurii
I now have seen the mistake I was doing. I had a iterate service where I was sending and flooding the requests down to the server code to process. This resulted in the too many requests error. I believe what I need is for the server code to Get the array of items from the LSV, iterate and then process them. Please take a look at my current server code. It currently only processes the last item in my LSV. So please could you give me the code to
Get the array of items from the LSV called “productcodes”
Iterate and process them
var productqty = 0
var DB_id = "XXXxx";
var collectionName = "YYyy";
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 updateProductQty = request.get("updateProductQty");
var productqty = request.get("updateProductQty");
var itemName = request.get("itemName");
var Product_Desc = request.get("Product_Desc");
if (productqty < 1)
{ productqty = 1
};
var productqty = parseInt(productqty);
//code to extract UPC numbers
var url = productid
var matchedPos = url.search("/50");
var matched = url.substr(matchedPos);
var num = matched.split("/")[1];
var barcode = parseInt(num);
//end
var query = [] ;
try
{
// result = {};
var result = [];
var params = {};
params.criteria =
{
Barcode: productbarcode
};
query = Collection.query(DB_id,collectionName,params);
if (query.length === 0)
{
Code: Select all
Collection.createObject(DB_id, collectionName, {
"Barcode": productbarcode, "UPCBarcode": barcode, "Product": newproductname, "Qty_in_Cup": (parseInt(productqty)), "Qty_Max": (parseInt(productqty) + 1), "Consumed": 1, "Restock_Level": 0, "Restock_Flag": (parseInt(productqty)), "Restock_Qty": 1, "ASAcin": productbarcode, "deptName": deptName, "shelfid": shelfid, "CrossOff_SL": false, "itemName": itemName, "Product_Desc": Product_Desc, "InvisableinCSList": false, "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, "itemName": itemName, "Product_Desc": Product_Desc,"InvisableinCSList": false, "CrossOff_SL": false, "Substitutions": false
Code: Select all
});
}
response.success(result);
}
} catch (e)
{
Code: Select all
response.success("message: " + e.message + "ncode: " + e.code);
}
Hi Mark,
Unfortunatly we don't have such code.
You need:
Collect all items to update/add in some array on client side.
Pass all these items to server code.via request parameter.
In server code - iterate through all received items.
For each item - make logic that you implement in this server code(above).
Regards.
Hi Yurii
Thanks for your reply. How do I iterate through request parameter? For example lets say the parameter is called productbarcode. What is the code to iterate the productbarcode parameter ?
I suppose what I should say is I have completed steps 1 & 2 above but I don't know the code to iterate through request parameter when it has multiple items. If you or anyone else on the support team could help me with this please