Page 1 of 2

Querying from Server code using Collection.Query

Posted: Mon Aug 25, 2014 7:22 pm
by Shawn

Writing a server code service where I'm trying to query a collection on a column using a request parameter 'grocerymasterid'. Here's the code and it returns all records if I remove the params but gives me an empty string if I add it in

var gmid = request.get("grocerymasterid") ;
var userName = request.get("userName"); //Store the user name
var userPass = request.get("userPass"); //and its password

var dbId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; // commented out
var collectionName = "Grocery" ;
var query = [] ;

try
{
result = {};

Code: Select all

var token = DatabaseUser.login(dbId, userName, userPass).sessionToken;  

var params = {};

Code: Select all

params.criteria =  
{   
            'grocerymasterid' :  gmid       
}; 

query = Collection.query(dbId,collectionName,params,token) ;

Code: Select all

response.success(query); 

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


Querying from Server code using Collection.Query

Posted: Mon Aug 25, 2014 8:28 pm
by Shawn

forgot to mention grocerymasterid is a pointer column


Querying from Server code using Collection.Query

Posted: Tue Aug 26, 2014 3:23 am
by Yurii Orishchuk

Hi Shawn,

You need to use "$inQuery" criteria in you case.

Here is right code for you:

pre

var gmid = request.get("grocerymasterid");

var userName = request.get("userName"); //Store the user name
var userPass = request.get("userPass"); //and its password

var dbId = "";// commented out
var collectionName = "Grocery " ;
var query = [] ;

try
{
result = {};

var token = DatabaseUser.login(dbId, userName, userPass).sessionToken;

var params = {};

params.criteria =
{
grocerymasterid: {"$inQuery": {_id: gmid } }
};

query = Collection.query(dbId,collectionName,params,token) ;

response.success(query);
}
catch (e)
{
response.success("message: " + e.message + "ncode: " + e.code);
}

/pre

Also, here is details about "$inQyery" clause: http://devcenter.appery.io/documentat...

Regards.


Querying from Server code using Collection.Query

Posted: Tue Aug 26, 2014 1:31 pm
by Shawn

That's awesome thanks yurii ... after I get the query array I want to loop through and copy those records and create new ones .. so this is what I'm doing ...

Code: Select all

for(var i=0;i  

Any ideas how I map the value in query array to the column ?


Querying from Server code using Collection.Query

Posted: Wed Aug 27, 2014 12:09 am
by Yurii Orishchuk

Hi Shawn,

Unfortunatly you code is not visible for us.

Please pre tags to wrap your code:

pre

<pre

</pre

also you need to replace all "<" with "&" to display it correctly in GS answer editor&#46;

/pre

Regards.


Querying from Server code using Collection.Query

Posted: Thu Aug 28, 2014 10:16 am
by Shawn

ok here it is ...

pre
query = Collection&#46;query(dbId,collectionName,params,token) ;

Code: Select all

for(var i=0;i&query&#46;length;i++)  
{ 

Collection&#46;createObject(dbId, collectionName,
{
"grocerymasterid" : { collName : "GroceryMaster", id : newgmid} ,
"userid" : { collName : "users", id : query&#46;userid }
})&#46;
id ;

Code: Select all

} 

/pre


Querying from Server code using Collection.Query

Posted: Fri Aug 29, 2014 7:33 pm
by Kateryna Grynko

Hi Shawn,

Have you defined 'newgmid' variable? Also please check how you write 'userid' (it is case-sensitive), does it have the same name in collection?


Querying from Server code using Collection.Query

Posted: Fri Aug 29, 2014 8:46 pm
by Shawn

Hi Katya

Yes I've defined newgmid

You can check the server code here
https://appery.io/servercode/ccd2c1b2...

This is the DB in which I'm trying to create the records
https://appery.io/database/53e542efe4...

As you can see the column names match...


Querying from Server code using Collection.Query

Posted: Sat Aug 30, 2014 8:14 am
by Alena Prykhodko

Shawn,

Please share you server code script and database with a href="mailto:support@appery.io" rel="nofollow"support@appery.io/a and tell us their names.


Querying from Server code using Collection.Query

Posted: Tue Oct 07, 2014 2:59 pm
by Kateryna Grynko

Hi Shawn,

Did you get it working?