如何在parse.com上更新离线用户记录?

时间:2014-10-10 14:16:30

标签: node.js parse-platform

我正在开发一个推荐系统,我将增加邀请者的邀请计数器。

我做了很多搜索,但没有找到有效的例子。

Bellow是代码

app.get('/increase-counter', function( request, response ) {




    //Sdk Link: http://blog.parse.com/2012/10/11/the-javascript-sdk-in-node-js/

    var Parse = require('parse-cloud').Parse;
    Parse.initialize( config.parse.appId, config.parse.jsKey );

    Parse.Cloud.define("modifyUser", function(request, response) {
        console.log('hello4');

      if (!request.user) {
        response.error("Must be signed in to call this Cloud Function.")
                console.log('hello5');
        return;
      }
      // The user making this request is available in request.user
      // Make sure to first check if this user is authorized to perform this change.
      // One way of doing so is to query an Admin role and check if the user belongs to that Role.
      // Replace !authorized with whatever check you decide to implement.
      if (!authorized) {
                console.log('hello6');
        response.error("Not an Admin.")
        return;    
      }

      // The rest of the function operates on the assumption that request.user is *authorized*

      Parse.Cloud.useMasterKey();

      // Query for the user to be modified by username
      // The username is passed to the Cloud Function in a 
      // key named "username". You can search by email or
      // user id instead depending on your use case.

      var query = new Parse.Query(Parse.User);
      //query.equalTo("username", request.params.username);
      query.equalTo("objectId", req.query.objectId);          

    console.log('hello7');

      // Get the first user which matches the above constraints.
      query.first({
        success: function(anotherUser) {
                    console.log('hello8',anotherUser);
          // Successfully retrieved the user.
          // Modify any parameters as you see fit.
          // You can use request.params to pass specific
          // keys and values you might want to change about
          // this user.
          anotherUser.set("inviteCounter", 2);

          // Save the user.
          anotherUser.save(null, {
            success: function(anotherUser) {
              // The user was saved successfully.
              response.success("Successfully updated user.",anotherUser);
            },
            error: function(gameScore, error) {
              // The save failed.
              // error is a Parse.Error with an error code and description.
              response.error("Could not save changes to user.",gameScore,error);
            }
          });
        },
        error: function(error) {
          response.error("Could not find user.",error);
        }
      });
    });

    console.log('hello1');

    Parse.Cloud.run('modifyUser', { objectId: request.query.objectId }, {
      success: function(status) {
                console.log('hello2');
        // the user was updated successfully            

      },
      error: function(error) {
                console.log('hello3', error);
        // error
      }
    }); 

});

当我运行url时,它在终端显示结果:

hello1
hello3 { code: 141, message: 'function not found' }

请告诉我如何更新inviteCounter?

0 个答案:

没有答案