Parse.com on Signup用户名已存在,代码142

时间:2015-04-24 15:25:59

标签: javascript parse-platform

更新:已解决,恰好是我写的云代码不正确,请参阅更新#3

我正在尝试使用Javascript中的Parse.com API为用户注册。 仍然没有成功。

基本上我有一个用户字段和一个密码字段,当点击注册按钮时,尝试登录但总是有“错误代码:142:用户名已存在,请尝试登录错误来自解析api。

代码有什么问题? (我之前使用过c#代码而且成功了) 谢谢,谢谢。

function onSignupButtonClicked(button) {
    event.preventDefault();

    game_username = $("#loginUserText").val();
    game_password = $("#loginPasswordText").val();

    parseLogout(); // tried both logout earlier or put here..

    var user = new Parse.User();
    user.set("username", game_username);
    user.set("password", game_password);


    var progressCircle = showProgressCircle(); //some fullscreen progress

    //also tried user.signup(null, {.. with no luck

    Parse.User.signUp(game_username, game_password, {}, {
        success: function(user) {
            // Hooray! Let them use the app now.
            //NOW LOGIN

            login(game_username, game_password).then(function(result) {
                console.log(result); // "Stuff worked!"
                hideProgressCircle(progressCircle);

                $("#loginButton").attr("disabled", true);
                $("#logoutButton").attr("disabled", false);
                $("#signupButton").attr("disabled", true);

                game_manualLogin = true;
                isLoggedIn = true;


            }, function(err) {
                hideProgressCircle(progressCircle);

                console.log("loginPromise: " + err.message + " code: " + err.code); // Error: "It broke"
                   handleParseError(err);

            });

        },
        error: function(user, error) {
            // Show the error message somewhere and let the user try again.
                 hideProgressCircle(progressCircle);
               handleParseError(error);

        }
    });

}

更新: 即使是基本的Parse.com javascript返回142 ..这里是代码,我的位置是伊斯坦布尔/土耳其

function register() {

    var user = new Parse.User();
    user.set("username", "testop");
    user.set("password", "testop");
    user.set("email", "email@example.com");

    // other fields can be set just like with Parse.Object
    user.set("phone", "415-392-0202");

    user.signUp(null, {
        success: function(user) {
            // Hooray! Let them use the app now.
            console.log("testop register ok!");

        },
        error: function(user, error) {
            // Show the error message somewhere and let the user try again.
            alert("Error: " + error.code + " " + error.message);
        }
    });


}

这是我在上面更新的代码中遇到的错误 错误:代码:142:用户名已存在,请尝试登录(代码来自Parse Javascript Signup

更新#2: 即使Rest API也会出错:

MacBook-Pro:~ gg$ curl -X POST \
>   -H "X-Parse-Application-Id: MYAPPID" \
>   -H "X-Parse-REST-API-Key: MYRESTAPIKET" \
>   -H "X-Parse-Revocable-Session: 1" \
>   -H "Content-Type: application/json" \
>   -d '{"username":"testdude","password":"tesdude","phone":"415-392-0202"}' \
>   https://api.parse.com/1/users
{"code":142,"error":"User name already exists, try login"}
MacBook-Pro:~ gg$ 

更新#3: 用3 apis检查后,发现问题是我的错。 我在用户保存时忘记了一个Cloud Code,因为它没有检查查询长度,所以它有问题。

Parse.Cloud.beforeSave(Parse.User, function(request, response) {
    var username = request.object.get("username");

    var usernamequery = new Parse.Query(Parse.User);
    usernamequery.equalTo("username", username);

    usernamequery.find({
        success: function()
        {
          --->>>should be checking length of query here  <<<---
         console.log("same user name found");
         response.error("User name already exists, try login");
        },
        error: function(error)
        {
         console.log("ok unique user name continue save");
         response.success("OK saving user");
        }   
    });


});

1 个答案:

答案 0 :(得分:0)

您需要两个表单,一个用于注册(作为新用户),调用Parse.User.signUp,另一个表单(用于返回用户)调用Parse.User.logIn。

问题是您只能使用用户名和密码“注册”一次,之后您应该“登录”。此外,当您从Parse.User.signUp获得成功回调后,用户就会登录,您无需拨打“登录”。

像这样的东西(两个事件监听器由两个不同的按钮触发)。

SIGNUP

function onSignupButtonClicked(button) {
    event.preventDefault();

    game_username = $("#loginUserText").val();
    game_password = $("#loginPasswordText").val();

    Parse.User.signUp(game_username, game_password, {}, {
        success: function(user) {
            // A new user has signed up and is now the Parse.User.current() user
            // Do something
        },
        error: function(user, error) {
            // Show the error message somewhere and let the user try again.         
        }
    });

}

LOGIN

function onLoginButtonClicked(button) {
    event.preventDefault();

    game_username = $("#loginUserText").val();
    game_password = $("#loginPasswordText").val();

    Parse.User.logIn(game_username, game_password, {}, {
        success: function(user) {
            // An existing user has logged in and is now the Parse.User.current() user
            // Do something
        },
        error: function(user, error) {
            // Show the error message somewhere and let the user try again.
        }
    });

}

此处有更多信息:https://parse.com/docs/js_guide#users-signup