Page 1 of 2

Using Server Code to update multiple objects

Posted: Thu Aug 11, 2016 2:59 pm
by Ellen Schlechter

I am using a code snippet to update multiple records once per day.

This is how I have customized it:

var today = new Date ();
var result = Collection.multiUpdateObject(dbId, collectionName, '{'vaccines': {"$gte": today}} ', {"vac_test": false});

I have a day stored in the vaccines column of the Appery.io database as a string. This is its format: Tue Aug 23 2016 00:00:00 GMT-0500 (CDT)
I basically want to update the vac_test boolean if today's date is after the date in the database.

I am not sure that I fully understand how this works.

  1. Do I have to replace dbId and collectionName or is that automatically found?

  2. I get an error when I test this:
    Script auto_shot_update: SyntaxError: missing ) after argument list ( @ 31 : 64 ) - var result = Collection.multiUpdateObject(dbId, collectionName, '{'vaccines': {"$gte": today}} ', {"vac_test": false});

    I have shared this with support and it is called "auto_shot_update" if you need to look at it.

    This is not user specific, but it will query the entire "Calf_data" collection.


Using Server Code to update multiple objects

Posted: Thu Aug 11, 2016 7:34 pm
by maxkatz

Here is an example updating multiple objects using Server Code API: https://docs.appery.io/reference#serv.... The database API key (ID) and collection name need to bet set by you.


Using Server Code to update multiple objects

Posted: Thu Aug 11, 2016 8:41 pm
by Ellen Schlechter

Okay I now have the two variables to define the database and and collection name but I still get the same error.

var dbId = "5c1910ae4b04e2d7216ee8f";
var collectionName = "Calf_data" ;
var today = new Date ();
var result = Collection.multiUpdateObject(dbId, collectionName, '{'vaccines': {"$gte": today}} ', {"vac_test": false});


Using Server Code to update multiple objects

Posted: Thu Aug 11, 2016 8:55 pm
by maxkatz

You need to make sure the date is in the right format. Before setting the date in runtime, set a static value to test the query. Once that works you will need to format the date to the same string.


Using Server Code to update multiple objects

Posted: Thu Aug 11, 2016 9:00 pm
by Ellen Schlechter

I tried making 'today' the date like what is in the database, a date formatted in different orders (mm/dd/yyyy, yy/mm/dd), and also just a simple number. Not one of them worked.


Using Server Code to update multiple objects

Posted: Thu Aug 11, 2016 11:17 pm
by maxkatz

What is the type of your "today" column?


Using Server Code to update multiple objects

Posted: Fri Aug 12, 2016 1:47 am
by Ellen Schlechter

I don't have a today column. It is just the variable that holds today's that is generated with new Date ();


Using Server Code to update multiple objects

Posted: Fri Aug 12, 2016 11:38 am
by Illya Stepanov

You can use console.log() to see the actual date value that you'd stored in your variable, by default it is not corresponds to our database date format so you will need to modify it.


Using Server Code to update multiple objects

Posted: Fri Aug 12, 2016 1:56 pm
by Ellen Schlechter

I am now trying to store it as mm/dd/yyyy so it will hopefully be easier. But, I am having trouble adding 14 days to it. At one point I almost had it but it didn't take into account when the month needs to change. Any ideas on how to add 14 days?


Using Server Code to update multiple objects

Posted: Fri Aug 12, 2016 7:35 pm
by maxkatz

Check out the Date JavaScript object, it has many different methods to help you manipulate the date: https://developer.mozilla.org/en-US/d....

You could also look at using a library such as http://momentjs.com/ .