0

I've created a page for signing up with parse, this is my code where I left the initialization out intentionally:

<input type="text" id="userInput" placeholder="Username" />
<input type="text" id="passInput" placeholder="Password" />
<input type="text" id="emailInput" placeholder="E-Mail" />
<input type="submit" onclick="signUp()" />

<script>
function signUp() {	
    var user = document.getElementById("userInput").value;
    var pass = document.getElementById("passInput").value;
    var email = document.getElementById("emailInput").value;
    // Create a new instance of the user class
    var user = new Parse.User();
    user.set("username", user);
    user.set("password", pass);
    user.set("email", email);
    // other fields can be set just like with Parse.Object
  
    user.signUp().then(function(user) {
        console.log('User created successful with name: ' + user.get("username") + ' and email: ' + user.get("email"));
    }).catch(function(error){
        console.log("Error: " + error.code + " " + error.message);
    });
}
</script>

but for some reason it returns with the Error that no username is entered, Thank you in advance.

1
  • Can you please share the actual error and your console logs? Commented Mar 9, 2020 at 1:28

2 Answers 2

1

Variable user is declared on the below line to get and store the value from the userInput field.

var user = document.getElementById("userInput").value;

However, in the below line, you are overriding the above variable declaration.

 var user = new Parse.User();

now if you try a console.log(user) at this point, you will find that the variable user will not contain the value of the document.getElementById("userInput").value; but will contain the instance of User

That's why you will not get the right value when you try to run user.set("username", user);

use ES6 let while declaring variables, which will help you capture such issues while compiling and rectify it sooner.

For the current issue, just modifying the variable name should fix your problem.

let userInput = document.getElementById("userInput").value;

user.set("username", userInput );

Cheers.

Sign up to request clarification or add additional context in comments.

Comments

0

Here you set the var user : var user = document.getElementById("userInput").value;

then here you set the var user : var user = new Parse.User();

so when you do this : user.set("username", user);, you are actually passing in the Parse.User user variable

try this :

var userInput = document.getElementById("userInput").value;
var pass = document.getElementById("passInput").value;
var email = document.getElementById("emailInput").value;
// Create a new instance of the user class
var user = new Parse.User();
user.set("username", userInput );

Comments

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.