Ellen Schlechter
Posts: 0
Joined: Sun Aug 31, 2014 3:28 am

Using Server Code to update multiple objects

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.

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Using Server Code to update multiple objects

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.

Ellen Schlechter
Posts: 0
Joined: Sun Aug 31, 2014 3:28 am

Using Server Code to update multiple objects

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

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Using Server Code to update multiple objects

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.

Ellen Schlechter
Posts: 0
Joined: Sun Aug 31, 2014 3:28 am

Using Server Code to update multiple objects

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.

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Using Server Code to update multiple objects

What is the type of your "today" column?

Ellen Schlechter
Posts: 0
Joined: Sun Aug 31, 2014 3:28 am

Using Server Code to update multiple objects

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

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

Using Server Code to update multiple objects

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.

Ellen Schlechter
Posts: 0
Joined: Sun Aug 31, 2014 3:28 am

Using Server Code to update multiple objects

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?

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Using Server Code to update multiple objects

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

Return to “Issues”