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 »

Hello Yurii
Thank you for your reply I think I have seen the problem that I am having. If we are processing a single product the processing of the product works fine. So that is a button is clicked, the query service runs, if the product exists on the database the update service runs, if the product does not exist on the database the create service runs. However if there are multiple products to be processed and some products need to run the query service and then the update service runs & some products need to run the query service and then the create service runs, the process screws up. Its seems that what is happening is the all products (lets say 10 for example) will run the query service before the create or update service begins. So I need the instructions on how to process the first product (the query service runs, if the product exists on the database the update service runs, if the product does not exist on the database the create service runs.) then the second product (the query service runs, if the product exists on the database the update service runs, if the product does not exist on the database the create service runs) etc

Can you help?

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 described in previous message common plan to process single object.

If you need two or more - you need iterate same process with other objects you need to create/update.

Also if you have real 10 objects to process i can suggest you to implement all this logic in server code. Here is details about it: https://devcenter.appery.io/documenta...

In this case(if you willing to use server code) you will have just one request from client part to server part and it will save you a time.

Regards.

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

Create or update database from array items

Post by Mark7294305 »

Hi Yurii
Ok I will look at the server code method. 10 objects was just an example. The objects can be from 1 to 200 on a invoice. Do you have any examples guides of processing multiple items using server code that I could read through?

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,

Unfrortunatly at this time we don't have such server code example.

But here is common plan to implement it:

  1. Pass to the script array of objects.(you need to make sure you pass it correctly).

  2. Iterate this array of objects.(1s step).

  3. Inside each iteration you need to perform:
    3.1. make query to DB collection to get all items with query criteria data from current item(3rd step).
    3.2. if you received 0 items in response of query you need to create object.
    3.3. if you received 1 item or more in response of query - you need to update object.

    Please learn more about server code here: https://devcenter.appery.io/documenta...

    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 have created the server code as follows

Params:
productbarcode
productid
updateproductname
newproductname

server code:
var DB_id = "XXX";
var collectionName = "collection";
var productid = request.get("productid");
var updateproductname = request.get("updateproductname");
var newproductname = request.get("newproductname");
var productbarcode = request.get("productbarcode");

var query = [] ;

try

{

// result = {};
var result = [];
var params = {};

params.criteria =

{

Code: Select all

 Barcode: productbarcode 
// _id: productid 

};

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

for (var i=0; i < query.length; i++) {
result.push ({Barcode:query.Barcode, id: query.id, Product: query.Product, Qty_in_Cup:query.Qty_in_Cup });

//if ( result===null )
if (result.length===0)
//if (response == '')
// if (response.body == 0)

//if(data == null || data.length < 1)

Code: Select all

{ 

 Collection.createObject(DB_id,collectionName, {"Product":newproductname})._id; 

}
else
Collection.updateObject(DB_id, collectionName, productid, {"Product":updateproductname});
}
response.success(result);

}

catch (e)

{
// Collection.createObject(DB_id,collectionName, {"Product":newproductname})._id;
response.success("message: " + e.message + "ncode: " + e.code);
//response.success("message: " + "Product Not found in Cupboard store");

}

The server code above can update 1 item but can not update multiple items or create new items. Can you help me?

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,

As i can see you use incorrect condition(cause of result.length always will be more then 0 at this point):

pre

if (result&#46;length===0)

/pre

You need to replace it with:

pre

if (query&#46;length === 0)

/pre

So please try following JS Code:

pre

var DB_id = "XXX";
var collectionName = "collection";
var productid = request&#46;get("productid");
var updateproductname = request&#46;get("updateproductname");
var newproductname = request&#46;get("newproductname");
var productbarcode = request&#46;get("productbarcode");

var query = [];

try

{

Code: Select all

 &#47;&#47; result = {};  
 var result = []; 
 var params = {}; 

 params&#46;criteria = 

     { 

     Barcode: productbarcode 
     &#47;&#47; _id: productid  

 }; 

 query = Collection&#46;query(DB_id, collectionName, params); 

 for (var i = 0; i < query&#46;length; i++) { 
     result&#46;push({ 
         Barcode: query[i]&#46;Barcode, 
         _id: query[i]&#46;_id, 
         Product: query[i]&#46;Product, 
         Qty_in_Cup: query[i]&#46;Qty_in_Cup 
     }); 

     &#47;&#47;if ( result===null )  
     if (query&#46;length === 0) 
         &#47;&#47;if (response == '')  
         &#47;&#47; if (response&#46;body == 0)  

         &#47;&#47;if(data == null || data&#46;length < 1)  

     { 

         Collection&#46;createObject(DB_id, collectionName, { 
             "Product": newproductname 
         }); 

     } else Collection&#46;updateObject(DB_id, collectionName, productid, { 
         "Product": updateproductname 
     }); 
 } 
 response&#46;success(result); 

} catch (e)

{
&#47;&#47; Collection&#46;createObject(DB_id,collectionName, {"Product":newproductname})&#46;_id;
response&#46;success("message: " + e&#46;message + "ncode: " + e&#46;code);
&#47;&#47;response&#46;success("message: " + "Product Not found in Cupboard store");

}

/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
I have copied your server code. However when I execute a new product I receive the following error

message: Invalid object ID: ''ncode: DBSU210

The Collection.createObject is not executed and the new product is not added to the database

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

Create or update database from array items

Post by Mark7294305 »

Hi Yurii
I have made some progress on this. The is the code I am currently using

var DB_id = "XXX";

var collectionName = "collection";

var productid = request.get("productid");

var updateproductname = request.get("updateproductname");

var newproductname = request.get("newproductname");

var productbarcode = request.get("productbarcode");

var query = [];

try

{

Code: Select all

 var result = []; 

 var params = {}; 

 params.criteria = 

     { 

     Barcode: productbarcode 

 }; 

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

     if (query.length === 0) 

     { 

         Collection.createObject(DB_id, collectionName, { 

             "Product": newproductname 

         }); 

     } else 

for (var i=0; i < query.length; i++) {
Collection.updateObject(DB_id, collectionName, productid, {

Code: Select all

         "Product": updateproductname 

     }); 

 } 

 response.success(result); 

} catch (e)

{

Code: Select all

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

}

The above code will create new items and update existing items as required however it only processes 2 items then stops. So for example lets say there are 6 items to be processed, (3 to be created and 3 to be updated), it will randomly choose any 2 items (it could be both new or both updates or 1 each) and then process them :-( Can you help?

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,

You update always one object with this line of code:

pre

&#47;&#47;productid here always direct to one object&#46;
Collection&#46;updateObject(DB_id, collectionName, productid&#46;&#46;&#46;&#46;

/pre

You need to get each iteration new "_id" parameter.
Here is a modified code:

pre

var DB_id = "XXX";

var collectionName = "collection";

var productid = request&#46;get("productid");

var updateproductname = request&#46;get("updateproductname");

var newproductname = request&#46;get("newproductname");

var productbarcode = request&#46;get("productbarcode");

var query = [];

try

{

Code: Select all

 var result = []; 

 var params = {}; 

 params&#46;criteria = 

     { 

     Barcode: productbarcode 

 }; 

 query = Collection&#46;query(DB_id, collectionName, params); 

 if (query&#46;length === 0) 

 { 

     Collection&#46;createObject(DB_id, collectionName, { 

         "Product": newproductname 

     }); 

 } else { 
     for (var i = 0; i < query&#46;length; i++) { 
         var currentObject = query[i]; 

         Collection&#46;updateObject(DB_id, collectionName, currentObject["_id"], { 

             "Product": updateproductname 

         }); 

     } 
 } 

 response&#46;success(result); 

} catch (e)

{

Code: Select all

 response&#46;success("message: " + e&#46;message + "ncode: " + e&#46;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
THANK YOU SO MUCH!! (I've been stuck on this for over 3 weeks). One more question. Is it possible to add the values of 2 parameters together? so for example

Collection.updateObject(DB_id, collectionName, currentObject["_id"], {

"Qty_in_Cup": productqty + stockqty

Post Reply