0

I am trying to write a simple Cloud Code function on Parse Server that updates a user's paramater. My function is as follows:

Parse.Cloud.define("updateUser", function(request, response) {
    var query = new Parse.Query("User");
    query.equalTo("username", request.params.username);
    var type = request.params.type;
    query.first({
        success: function (user) {
            user.set("userType", type);
            user.save(null, {
                success: function (object) {
                    response.success("SUCCESS");
                }, error: function (object, error) {
                    response.error(error);
                }
            });
        }, error: function (error) {
            response.error(error);
        }
    });
});

And this is how I call the function server-side from an Express Node.js app:

Parse.Cloud.run("updateUser", { useMasterKey: true, username: req.body.username, type: req.body.type }, {
    success: function(final) {
        res.send({ res: "SUCCESS" });
    }, error: function(error) {
        res.send({ res: error });
    }
});

The first query returns a user, but the subsequent save throws a "206: cannot modify user" error.

This function worked great on our previous platform, but now is causing issues on Parse Server.

Any help is greatly appreciated.

  • You have to use master key, they changed how you deal with master key in cloud code with parse-server... Check their github – Mazel Tov Jul 22 '16 at 12:30
  • I did specify the master key when I call the function - { useMasterKey: true } ... this is per their documentation – Kevin Lyons Jul 22 '16 at 13:29

2 Answers2

1

the useMasterKey should be send to the user.save() of your cloud code method and in your code you sent the useMasterKey from your nodeJS app.

so at the end your code should look like the following:

Parse.Cloud.define("updateUser", function(request, response) {
var query = new Parse.Query("User");
query.equalTo("username", request.params.username);
var type = request.params.type;
query.first({
    success: function (user) {
        user.set("userType", type);
        user.save({useMasterKey : true}, {
            success: function (object) {
                response.success("SUCCESS");
            }, error: function (object, error) {
                response.error(error);
            }
        });
    }, error: function (error) {
        response.error(error);
    }
});

});

I would also change my code to use Promises and arrow functions according to the best practices.

Hope it helps.

Ran Hassid
  • 2,788
  • 1
  • 12
  • 20
0

There is a different implementation for it without cloud function. Since it is the current logged in client that will update its "lastActivity" status, You can create a simple method that like this:

_lastActivity = () => {
    var currentUser = Parse.User.current();
    currentUser.set("lastActive", new Date());
    currentUser
      .save()
      .then(result => {
        console.log(result);
      })
      .catch(error => {
        console.log(error);
      });
  };

Now this method an be called at some points when user interacts with your application like: swipes screen, or refreshes a ScrollView component...Or you can call it at intervals as long as their session is active... you know what works best.

Then you can write a query to get all users that are not the current user depending if you want to populate it on a FlatList component. when you save the result in a state, you can then simply use the "lastActive" data from the state by,

Using Icons:

 <Ionicons name="md-radio-button-on" size={15} 
color={new Date() -item.get("lastActive") < 600000? "green": "red"}/>

Text beside the Icon:

<Text style={{fontWeight:"400",color: "#666",fontSize: 12}}>
  {new Date() - item.get("lastActive") < 600000? "online": "offline"}</Text>

Click to see: Image of How this Implementation looks