简体   繁体   English

无法让用户在注册过程中上传个人资料图片(Parse.com/JS SDK)

[英]Unable to let user upload profile picture in sign up process (Parse.com/JS SDK)

Using parse.com and the JavaScript SDK. 使用parse.com和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 更新了,这是我正在使用的代码,它在尝试注册用户时返回错误控制台消息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) {
> 
>         }
>     });

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 parse.com用户注册无法正常工作的问题

   $('#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.

    }
  });


};
}

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. 那么你检查你的val是未定义的还是null。

if(ifUndeforNull(userFirstname)){ 
   console.log('User First Name Null or Undefined');
}

wrapping the entirety in a try{} catch{} with logging would also help. 将整个包装在try {} catch {}中并使用日志记录也会有所帮助。

Lastly, I'd declare the variables such as userFirstName with var, as sometimes browsers can be annoyingly non-standard when that is not specified. 最后,我将使用var声明变量,例如userFirstName,因为有时候浏览器在未指定时会非常不标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM