0

I'm trying to update a Parse Server user from my client web app using the Javascript SDK.

I understand that this isn't possible using Parse.User but is possible using Parse.Object. I'm following the guidance to update Object but can't seem to get bast the 3rd line of the function.

function () {
    var updateTheUser = Parse.Object.extend("_User");
    var query = new Parse.User.current();
    query.get(Parse.User.current().id, {
        success: function (update) {
            // The object was retrieved successfully.
            update.set("phone", "01234 567890");
            update.set("Barred", false);
            update.save(null, {
                success: function (update) {
                    console.log("Updated!");
                            }
                        });
                    },
                error: function (object, error) {
                    // The object was not retrieved successfully.
                    // error is a Parse.Error with an error code and message.
                }
            });
        };
    };

3 Answers 3

1

Nested callbacks are a magnet for errors, as you can see in your code. The callback you think handles the object not retreived actually handles a failed save.

Try the following and see what that shows in your console (note: may not work in IE without babel):

const asPromise = (fn,context,args) => {
  return new Promise(
    (resolve,reject) => {
      args.concat(
        {
          success:resolve,
          error:(o,e) => reject([o,e])
        }
      );
      fn.apply(context,args);
    }
  );
};
var updateTheUser = Parse.Object.extend("_User");
var query = new Parse.User.current();
//maybe query should be:
// var query = new Parse.Query(User);
// from documentation: http://docs.parseplatform.org/js/guide/#retrieving-objects
asPromise(query.get,query,[Parse.User.current().id])
.then(
  update => {
    update.set("phone", "01234 567890");
    update.set("Barred", false);
    return asPromise(
      update.save,
      update,
      [null]
    )
  }
)
.then(
  user => console.log("updated user:",user)
  ,error => console.warn("Something went wrong:",error)
);
Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou for this. I believe that the issue is that the user can only be altered by the Master Key & so I understand that this is where the error is (the Javascript SDK only using the SessionToken for validation)
0

The only way that I've been able to solve this is by doing an AJAX request using the REST API

updateUser = function () {

    var userId = Parse.User.current().id;
    var data = JSON.stringify({
        "username": username,
        "firstname": firstname,
        "lastname": lastname,
        "email": email,
        "phone": phone
    });

    $.ajax({
        url: 'myServerURL' + userId,
        type: 'PUT',
        headers: {
            'X-Parse-Application-Id': "my_api",
            'X-Parse-Master-Key': "my_masterkey",
            'Content-Type': "application/json"
        },
        "data": data,
        success: function (result) {
            // Do something with the result
            alert("you have successfully updated your account.  We're now going to need you to log back in with your new details?");

            Parse.User.logOut();
            window.location.href = "/#/login";
        },
        error: function (Response) {
            alert(Response.error);
        }
    });
};

WARNING: As this contains the Master Key, it should be located in Cloud Code on your Parse Server, not in your web app

Comments

0

Only this should work:

var user = Parse.User.current();
user.set("phone", "01234 567890");
user.set("Barred", false);
user.save(null, {
    success: function (update) {
        console.log("Updated!");
    },
    error: function (error) {
        console.log(error);
    }
});

5 Comments

As per my comment on the other answer, I understand that the main problem is related to the authentification type.
The user has permission to update his own data. But anyway, if you want to do from cloud code (I've just got it in the answer below), you can just do update.save(null, { success: ..., error: ..., useMasterKey: true }).
I find that when I use the code as per your explanation, I get an authentication error, which from reading the documentation I understand that there is a block on editing user fields from the Javascript API. Does it work for you?
If you are updating the current user, yes. If you are updating another user, only if you initialize the SDK with master key and pass the useMasterKey: true option. It shouldn't be a problem in cloud code, for example. You just need to pass the useMasterKey: true option. I've jus noticed your code is using "var user = new Parse.User.current();" try to use only "var user = Parse.User.current();" instead (without "new") and "var user = request.user;" in the case you are running this code in cloud code. I will edit my answer as well.
I had mistyped. Just fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.