Create or update database from array items

This forum contains topics that were moved from Get Satisfaction
Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Create or update database from array items

Post 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

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,

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

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

Create or update database from array items

Post 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?

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,

Please show us these errors screen shots.

Thanks.

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

Create or update database from array items

Post 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

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,

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.

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

Create or update database from array items

Post 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); 

}

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,

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.

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

Create or update database from array items

Post 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 ?

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

Create or update database from array items

Post 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

Post Reply