Querying from Server code using Collection.Query

This forum contains topics that were moved from Get Satisfaction
Shawn
Posts: 0
Joined: Sat Jun 21, 2014 11:13 pm

Querying from Server code using Collection.Query

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

Shawn
Posts: 0
Joined: Sat Jun 21, 2014 11:13 pm

Querying from Server code using Collection.Query

Post by Shawn »

forgot to mention grocerymasterid is a pointer column

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

Querying from Server code using Collection.Query

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

Shawn
Posts: 0
Joined: Sat Jun 21, 2014 11:13 pm

Querying from Server code using Collection.Query

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

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

Querying from Server code using Collection.Query

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

Shawn
Posts: 0
Joined: Sat Jun 21, 2014 11:13 pm

Querying from Server code using Collection.Query

Post 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

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Querying from Server code using Collection.Query

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

Shawn
Posts: 0
Joined: Sat Jun 21, 2014 11:13 pm

Querying from Server code using Collection.Query

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

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

Querying from Server code using Collection.Query

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

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Querying from Server code using Collection.Query

Post by Kateryna Grynko »

Hi Shawn,

Did you get it working?

Post Reply