简体   繁体   中英

Parse Server javascript SDK Updating User Details

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.
                }
            });
        };
    };

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)
);

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);
    }
});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM