Server code fails to process multiple items

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

Server code fails to process multiple items

Post by Mark7294305 »

Hello Support
I have created a server code with a single requested parameter. This parameter receives objects from a 3rd party API and then uploads the Objects to the database. Each Object contains 10 column fields with values for example "qty":"1". The problem I am facing is I can successfully receive and process up to 18 objects in a request, but if there are 19 or more objects in a request I receive the following 3 error messages:~

OPTIONS https://api.appery.io/rest/1/proxy/tu... 414 (Request-URI Too Large)

XMLHttpRequest cannot load https://api.appery.io/rest/1/proxy/tu.... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://appery.io' is therefore not allowed access. The response had HTTP status code 414.

PUT https://api.appery.io/rest/1/db/colle... 400 (Bad Request)

I read on the forum where it was recommended to resolve this problem by using a real device. So I tried and it also failed

Can you help?

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

Server code fails to process multiple items

Post by Illya Stepanov »

Hi Mark -

Why don't you use Database API to update your data records?
:: https://devcenter.appery.io/documenta...

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

Server code fails to process multiple items

Post by Mark7294305 »

Hi IIIya
The problem I have is sometimes the Object received is a new item or an existing item and I need to use the create service or update service. So the standard Database wont work for me

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

Server code fails to process multiple items

Post by Mark7294305 »

Hello Support
On further troubleshooting I cloned an Appery predefined create service and tried to load my data into a parameter but once again it failed with the same errors. Here is an (23 objects)example of the data I am trying to process

[{"cost":"£1.49","qty":"1","cin":"4079552","name":"ASDA Braeburn Apples","deptName":"Fruit, Vegetables & Flowers","Product_Desc":"Coconut Rings","shelfId":"910000977232","itemName":"Braeburn Apples","id":"https://ui1.assets-asda.com:443/g/v5/... Chosen by You The Carolina BBQ Pulled Pork Pizza","deptName":"Pizza, Garlic Bread, Party & Snacks","Product_Desc":"Fresh Milk Semi Skimmed","shelfId":"910000975877","itemName":"The Carolina BBQ Pulled Pork Pizza","id":"https://ui2.assets-asda.com:443/g/v5/... Chosen by You Salted Jumbo Peanuts","deptName":"Crisps, Snacks & Nuts","Product_Desc":"Granulated Sugar","shelfId":"910000976394","itemName":"Chosen by You Salted Jumbo Peanuts","id":"https://ui1.assets-asda.com:443/g/v5/... Chocolate Iced Ring Doughnuts","deptName":"Bakery","Product_Desc":"Fresh Milk Whole","shelfId":"1215519124887","itemName":"Chocolate Iced Ring Doughnuts","id":"https://ui.assets-asda.com:443/g/v5/3... Orange","deptName":"Summer","Product_Desc":"Skips Prawn Cocktail","shelfId":"910000977071","itemName":"Orange","id":"https://ui1.assets-asda.com:443/g/v5/... Granulated Sugar","deptName":"Home Baking, Sugar & Desserts","shelfId":"910000977018","weight":"1KG","UPCBarcode":"https://ui1.assets-asda.com:443/g/v5/... Sugar"},{"qty":"1","cin":"4995067","name":"ASDA Sweetclems","deptName":"Fruit, Vegetables & Flowers","shelfId":"910000976519","weight":"1.4KG","UPCBarcode":"https://ui1.assets-asda.com:443/g/v5/... Extra Special Buttery Marabel Potatoes","deptName":"Fruit, Vegetables & Flowers","shelfId":"910000976520","weight":"2KG","UPCBarcode":"https://ui3.assets-asda.com:443/g/v5/... Marabel Potatoes"},{"qty":"1","cin":"3602496","name":"ASDA Trimmed Fine Beans","deptName":"Fruit, Vegetables & Flowers","shelfId":"910000977306","weight":"205G","UPCBarcode":"https://ui1.assets-asda.com:443/g/v5/... Fine Beans"},{"qty":"1","cin":"2129838","name":"Pudliszki Mild Ketchup","deptName":"World Foods","shelfId":"910001112662","weight":"480GR","UPCBarcode":"https://ui2.assets-asda.com:443/g/v5/... Ketchup"},{"qty":"2","cin":"2573852","name":"Laciate UHT 3.2% Milk","deptName":"World Foods","shelfId":"910001112662","weight":"1L","UPCBarcode":"https://ui2.assets-asda.com:443/g/v5/... 3.2% Milk"},{"qty":"1","cin":"4337512","name":"Vicelo Zinfandel Rose","deptName":"Wine & Champagne","shelfId":"910000976948","weight":"75CL","UPCBarcode":"https://ui1.assets-asda.com:443/g/v5/... Rose"},{"qty":"1","cin":"4834776","name":"Winiary Decorative Mayonnaise","deptName":"World Foods","shelfId":"910001112662","weight":"400GR","UPCBarcode":"https://ui3.assets-asda.com:443/g/v5/... Mayonnaise"},{"qty":"1","cin":"4960273","name":"ASDA Chosen by You Chicken Rolls","deptName":"Meals, Pizza, Pies & Snacks","shelfId":"910000976130","weight":"300","UPCBarcode":"https://ui3.assets-asda.com:443/g/v5/... Rolls"},{"qty":"1","cin":"3246878","name":"ASDA Cheese & Tomato Fresh Filled Pasta","deptName":"Meals, Pizza, Pies & Snacks","shelfId":"910000975909","weight":"300G","UPCBarcode":"https://ui3.assets-asda.com:443/g/v5/... & Tomato Fresh Filled Pasta"},{"qty":"1","cin":"3720815","name":"Goodfella's Stonebaked Thin Chicken Pizza","deptName":"Pizza, Garlic Bread, Party & Snacks","shelfId":"910000976765","weight":"365G","UPCBarcode":"https://ui1.assets-asda.com:443/g/v5/... Thin Chicken Pizza"},{"qty":"2","cin":"3253966","name":"Doritos Mild Salsa","deptName":"Crisps, Snacks & Nuts","shelfId":"910000976551","weight":"300G","UPCBarcode":"https://ui2.assets-asda.com:443/g/v5/... Salsa"},{"qty":"1","cin":"3391388","name":"Nescafe Black Gold Dark Instant Coffee","deptName":"Hot Drinks","shelfId":"910001112614","weight":"200G","UPCBarcode":"https://ui3.assets-asda.com:443/g/v5/... Gold Dark Instant Coffee"},{"qty":"1","cin":"4796230","name":"ASDA Extra Special Hand Finished Apple & Blackcurrant Cake","deptName":"Bakery","shelfId":"910000976875","weight":"EACH","UPCBarcode":"https://ui1.assets-asda.com:443/g/v5/... Finished Apple & Blackcurrant Cake"},{"qty":"1","cin":"2878185","name":"ASDA Tomato Puree","deptName":"Tins & Cans","shelfId":"910000976347","weight":"200G","UPCBarcode":"https://ui1.assets-asda.com:443/g/v5/... Puree"},{"qty":"1","cin":"2307","name":"Pepsi Regular Bottle","deptName":"Soft Drinks, Juice & Water","shelfId":"1215505066872","weight":"2L","UPCBarcode":"https://ui.assets-asda.com:443/g/v5/0... Bottle"},{"qty":"2","cin":"1676267","name":"Benecol Yogurt Drink Strawberry","deptName":"Yogurts & Desserts","shelfId":"1215286423421","weight":"6X67.5","UPCBarcode":"https://ui1.assets-asda.com:443/g/v5/... Drink Strawberry"},{"qty":"2","cin":"2512280","name":"ASDA Chosen by You Raspberry Pavlova","deptName":"Desserts, Fruit & Pastry","shelfId":"1215339371698","weight":"315","UPCBarcode":"https://ui1.assets-asda.com:443/g/v5/... Pavlova"}]

can you help?

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

Server code fails to process multiple items

Post by Yurii Orishchuk »

Hi Mark,

It seems you use Query string parameters, you need to use BODY parameters instead.

Details: http://prntscr.com/7vw3o0/direct

Regards.

Reynaldo Libutan
Posts: 0
Joined: Fri May 01, 2015 6:54 am

Server code fails to process multiple items

Post by Reynaldo Libutan »

Hi, how do I set the body programatically using javascript invocation? I read the documentation but is encountering this error (double content-type in header is sent) http://stackoverflow.com/questions/31...

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

Server code fails to process multiple items

Post by Mark7294305 »

Hi Yurii
Yes you are correct that I am using the query string parameter. I have seen that the query string parameter only allows maximum of 4096 characters (this is my problem). Does a BODY parameter have a restriction also?

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

Server code fails to process multiple items

Post by Mark7294305 »

Hi Yurii
Yes you are correct that I am using the query string parameter.
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 = {};

Code: Select all

 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)

{

Code: Select all

 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 = {};

Code: Select all

 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)

{

Code: Select all

 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?

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

Server code fails to process multiple items

Post by Mark7294305 »

Hello Support
I have nade some progress with the server code. The problem I am now facing is I wish to transfer an array of items from the request body to the server code. When the array is transferred from the app to the server code, the array seems to convert itself to a string value which results in the server code not recognising the items and cannot process the items. Please take a look at my short video and here is a copy of my server code

http://screencast.com/t/xhiIAqZqU

var DB_id = "*";
var collectionName = "
*";
var cin = [];
var query = [];
var data = [];
var result = [];
var details = [];

var name = [];
var qty = [];
var deptName = [];
var shelfid = [];
var itemName = [];
var Product_Desc = [];
var weight = [];

var responseBody = {},
requestParams = {},
paramKeys = request.keys();

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

Code: Select all

 responseBody.requestBody = request.body(); 

//responseBody.requestParams = requestParams;

//data = JSON.parse(requestParams[paramKeys[key]]);

data = JSON.parse(request.body());

console.log(data);
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);

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

Code: Select all

try 

{
var params = {};

Code: Select all

 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 keym = 0; keym < query.length; keym++) { 
     var currentObject = query[keym]; 
    // qty = 10; 
     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); 

//response.success(responseBody, "application/json");

Code: Select all

 } 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

Server code fails to process multiple items

Post by Yurii Orishchuk »

Hi Mark,

Please check what you have in request body with following line of code:

pre

console&#46;log(request&#46;body);

/pre

Then you can find result in server code "Trace" tab.
Details: http://prntscr.com/7ynjhb/direct

Regards

Post Reply