This is a migrated thread and some comments may be shown as answers.

Incrementing a field in Cloud code

2 Answers 137 Views
Cloud Code
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Kelly
Top achievements
Rank 1
Kelly asked on 14 Apr 2014, 02:20 AM
Hello:

I'm trying to get the mongo $inc operator to work in cloud code using the SDK's updateSingle call, and can't even get close.  A pointer to the correct syntax would be much appreciated.  All I'm trying to do is increment a field called Counter in a table called MyTable when a record is added to a content type. The id of the record to update is passed in the request as "uid".  The mongo update syntax doesn't seem to play well with the updateSingle syntax.  Here's where I'm at now:

Everlive.Events.afterCreate(function(request, response, context, done) {
    if (!response.error) {
      var options = {
        Id: request.uid,
        $inc: {
          Counter: 1
        }
      };
      var n = Everlive.Sdk.withMasterKey().data('MyTable').updateSingle(options,
                                   function (data) {
                                       console.log("update worked");
                                       done();
                                   },
                                                                           
                                    function (error) {
                                       console.log("failed: " + JSON.stringify(error));
                                       done();
                                    });
    }
  else {
    console.log("calling  done");
    done();
  }
});

This logs [2014-04-14 02:06:30.868] [INFO] update failed: {"message":"Error","code":901,"name":"Request error. HTTP status code: 400"}

Thank you!

Kelly

2 Answers, 1 is accepted

Sort by
0
Accepted
Anton Dobrev
Telerik team
answered on 14 Apr 2014, 10:32 AM
Hello Kelly,

Here is a simple reference for using some of the native Mongo operators with the JavaScript SDK:

Everlive.Events.beforeRead(function (request, context, done) {
  // ...
    var myData = Everlive.Sdk.withMasterKey().data('MyType');
 
    var attrs = {
        '$inc': {
            'Counter': 1
        }
    };
    var filter = {
        'Id': request.uid
    };
 
    myData.rawUpdate(attrs, filter,
        function (data) {
            console.dir(data);
            done();
        }, function (err) {
            console.dir(err);
            done();
        });
});

Please, pay attention to the rawUpdate() method as it allows the use of native operators. It is not documented yet, so please, accept our apologies for this inconsistency in our documentation.

On the other hand, the updateSingle() method internally is working as a $set operator.

Please, let us know if this works for you.

Regards,
Anton Dobrev
Telerik
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
0
Kelly
Top achievements
Rank 1
answered on 14 Apr 2014, 07:24 PM
That works perfectly, thank you Anton!
Tags
Cloud Code
Asked by
Kelly
Top achievements
Rank 1
Answers by
Anton Dobrev
Telerik team
Kelly
Top achievements
Rank 1
Share this question
or