Page 4 of 6

Create or update database from array items

Posted: Tue May 26, 2015 1:13 pm
by Mark7294305

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


Create or update database from array items

Posted: Fri May 29, 2015 1:31 am
by Yurii Orishchuk

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


Create or update database from array items

Posted: Fri May 29, 2015 8:01 am
by Mark7294305

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?


Create or update database from array items

Posted: Fri Jun 05, 2015 2:56 am
by Yurii Orishchuk

Hi Mark,

Please show us these errors screen shots.

Thanks.


Create or update database from array items

Posted: Fri Jun 05, 2015 9:37 am
by Mark7294305

Hi Yurii
Ok here is an example of 17 items I wish to upload the database.
Image

  1. I upload the items from a API to the App screen

    Image

    Here is the F12 screen

    Image

  2. I press upload to database and only 4 items are uploaded
    Image

    Here is the F12 screen


Create or update database from array items

Posted: Wed Jun 10, 2015 2:16 am
by Yurii Orishchuk

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.


Create or update database from array items

Posted: Wed Jun 10, 2015 10:46 am
by Mark7294305

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

  1. Get the array of items from the LSV called “productcodes”

  2. 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); 

}


Create or update database from array items

Posted: Fri Jun 12, 2015 2:09 am
by Yurii Orishchuk

Hi Mark,

Unfortunatly we don't have such code.

You need:

  1. Collect all items to update/add in some array on client side.

  2. Pass all these items to server code.via request parameter.

  3. In server code - iterate through all received items.

  4. For each item - make logic that you implement in this server code(above).

    Regards.


Create or update database from array items

Posted: Fri Jun 12, 2015 6:46 am
by Mark7294305

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 ?


Create or update database from array items

Posted: Sat Jun 13, 2015 7:34 am
by Mark7294305

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