Incrementing a field in Cloud code

3 posts, 1 answers
  1. Kelly
    Kelly avatar
    73 posts
    Member since:
    Apr 2011

    Posted 13 Apr 2014 Link to this post

    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. Answer
    Anton Dobrev
    Admin
    Anton Dobrev avatar
    539 posts

    Posted 14 Apr 2014 Link to this post

    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.
     
  3. Kelly
    Kelly avatar
    73 posts
    Member since:
    Apr 2011

    Posted 14 Apr 2014 in reply to Anton Dobrev Link to this post

    That works perfectly, thank you Anton!
Back to Top