OGeek|极客世界-中国程序员成长平台

标题: javascript - 使用 Cloud Code 和 PFFacebookUtils 覆盖电子邮件/用户名 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:25
标题: javascript - 使用 Cloud Code 和 PFFacebookUtils 覆盖电子邮件/用户名

在使用 Parse 服务时,我试图让用户跳过登录字段,方法是通过 Facebook 登录应用程序并使用 Facebook 信息填写字段。我正在使用 Parse 的 Cloud Code 来使 swift 客户端代码尽可能简单。但是,当我尝试使用云代码设置 Parse.User 的用户名和电子邮件地址字段时,我得到了来自 Parse.Cloud 的一些反馈。在应用内运行时出现以下错误。

[Error]: Can't modify username in the before save trigger (Code: 141, Version: 1.6.1)

当删除用户名功能时,我收到:

[Error]: Can't modify email in the before save trigger (Code: 141, Version: 1.6.1)

通过相同的代码,我还使用相同的方法设置 firstNamelastName 没有任何错误。下面是使用的代码。

[云代码]

function pad(num, size){ return ('000000000' + num).substr(-size); }

Parse.Cloud.beforeSave(Parse.User, function (request, response) {
    var token = request.object.get("authData").facebook.access_token
    Parse.Cloud.httpRequest({
        url: 'https://graph.facebook.com/v2.1/me?fields=first_name,last_name,email&access_token=' + token,
        success: function(httpResponse) {
             var responseData = httpResponse.data;

             request.object.set("email", responseData.email) // <- Error Occurs Here
             request.object.set("username", responseData.email.split("@")[0].concat(".", pad(Math.floor(Math.random()*10000),4))) // <- Error Occurs Here
             request.object.set("firstName", responseData.first_name)
             request.object.set("lastName", responseData.last_name)

             response.success()
        },
        error: function(){
            response.error("Something went wrong.")
        }
    })
})

[SWIFT 代码]

@IBAction func loginWithFacebookButtonAction(sender: AnyObject){
    loginWithFacebookButtonOutlet.setTitle("Logging In...", forState: UIControlState.Normal)
    let permissions = ["public_profile", "user_friends", "email"]
    PFFacebookUtils.logInWithPermissions(permissions, {
        (user: PFUser!, error: NSError!) -> Void in
        if user == nil {
            println("Uh oh. The user cancelled the Facebook login.")
        } else if user.isNew {
            println("User signed up and logged in through Facebook!")
            self.performSegueWithIdentifier("facebookToApp", sender: self)
        } else {
            println("User logged in through Facebook!")
            self.performSegueWithIdentifier("facebookToApp", sender: self)
        }
    })
}

我在这里遇到的其他问题是当用户尝试使用 .save() 而不是 response.success() 时引起的,这似乎没有是这种情况下的问题。

非常感谢。



Best Answer-推荐答案


似乎 username & email 不能在 beforeSave 触发器中更改,因为它们应该是唯一的

Right now, the email field cannot be modified on a beforeSave because this field should be unique. If you were to use a value that another user is already using, this would result in an error that your beforeSave method would not be able to handle.

引用:https://www.parse.com/questions/why-cant-i-modify-the-email-field-in-a-before-save-trigger

关于javascript - 使用 Cloud Code 和 PFFacebookUtils 覆盖电子邮件/用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27643307/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4