Reputation: 1932
Using parse.com and the JavaScript SDK. The code should let a user sign up and upload a profile picture.
Updated, this is the code I'm using that is returning an error when trying to sign up a user the console message is Uncaught TypeError: Cannot read property 'length' of undefined
> $('#SignUp').click(function(e) {
> UserSignUp(); });
>
>
> function UserSignUp() {
>
> var user = new Parse.User();
> userFirstname = $('#firstnamesu').val();
> userLastname = $('#lastnamesu').val();
> userUsername = $('#usernamesu').val();
> userGender = $('#gendersu').val();
> Email = $('#emailsu').val();
> PWP = $('#passwordsu').val();
>
> user.set("FirstName", userFirstname);
> user.set("LastName", userLastname);
> user.set("username", userUsername);
> user.set("gender", userGender);
> user.set("email", Email);
> user.set("password", PWP);
>
>
> var fileUploadControl = $("#pic")[0]; if (fileUploadControl.files.length > 0) { var file =
> fileUploadControl.files[0]; var name = "photo.png";
>
> var parseFile = new Parse.File(name, file);
>
> //put this inside if { parseFile.save().then(function() { //
> The file has been saved to Parse. }, function(error) { // The
> file either could not be read, or could not be saved to Parse.
> });
>
> // Be sure of ur parameters name
> // prod is extend of my class in parse from this: var prod = new products();
> user.set("ProfilePic", parseFile);
> user.save(); } ////////////Runs parse after the SignUp button has been clicked by the user////////////////////
>
> $('#SignUp').click(function(e) {
> UserSignUp(); });
>
> user.signUp(null, {
> success: function(user) {
> if (!user.existed()) {
> window.location.href = "user_home.html";
> } else {
> alert("NO WAY BUDDY");
> }
> },
> error: function(user, error) {
>
> }
> });
Upvotes: 12
Views: 353
Reputation: 1932
I was able to answer my own question by using the following code. Basically it was a case of refactoring the code into a more logical order that resolved it. The issue wasn't helped by another issue I had here which just confused matters further.Issue with parse.com user signup not working
$('#SignUp').click(function(e) {
UserSignUp();
});
function UserSignUp() {
var fileUploadControl = $("#pic")[0];
if (fileUploadControl.files.length > 0) {
var file = fileUploadControl.files[0];
var name = "photo.png";
var user = new Parse.User();
var parseFile = new Parse.File(name, file);
userFirstname = $('#firstnamesu').val();
userLastname = $('#lastnamesu').val();
userUsername = $('#usernamesu').val();
userGender = $('#gendersu').val();
Email = $('#emailsu').val();
PWP = $('#passwordsu').val();
user.set("ProfilePic", parseFile);
user.set("FirstName", userFirstname);
user.set("LastName", userLastname);
user.set("username", userUsername);
user.set("gender", userGender);
user.set("email", Email);
user.set("password", PWP);
var uri = encodeURI('http://XXXX.com/XXXX.html');
user.signUp(null, {
success: function(user) {
if (!user.existed()) {
window.location.href = uri;
}
},
error: function(user, error) {
// Show the error message somewhere and let the user try again.
}
});
};
}
Upvotes: 4
Reputation: 179
First suggestion, and it's going to make your code a bit more lengthy but it will show you where the error is occurring.
function ifUndeforNull(val){
var ret = false;
try{
if((val == undefined)|| (val==null)){
ret = true;
}
}catch(ex){
console.log("Error:"+ex);
}
return ret;
}
so then you check if your vals are undefined or null.
if(ifUndeforNull(userFirstname)){
console.log('User First Name Null or Undefined');
}
wrapping the entirety in a try{} catch{} with logging would also help.
Lastly, I'd declare the variables such as userFirstName with var, as sometimes browsers can be annoyingly non-standard when that is not specified.
Upvotes: 0