解析服务器 javascript SDK 更新用户详细信息

Parse Server javascript SDK Updating User Details

我正在尝试使用 Javascript SDK 从我的客户端 Web 应用更新 Parse Server 用户。

我知道使用 Parse.User 无法做到这一点,但可以使用 Parse.Object。我正在按照更新对象的指导进行操作,但似乎无法获取函数的第 3 行。

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

正如您在代码中看到的那样,嵌套回调很容易出错。您认为处理未检索对象的回调实际上处理失败的保存。

尝试以下操作并查看控制台中显示的内容(注意:在没有 babel 的情况下可能无法在 IE 中运行):

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

只有这个应该有效:

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

我能够解决这个问题的唯一方法是使用 REST API

执行 AJAX 请求
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);
        }
    });
};

警告:由于它包含主密钥,因此它应该位于您的 Parse Server 上的 Cloud Code 中,而不是您的网络应用程序中