控制台表示注册表单不存在onsubmit函数,尽管显然存在

时间:2019-05-06 04:09:18

标签: javascript html forms firebase google-cloud-firestore

因此,我有一个注册表,如下:

<form name="register" action="" method="POST" onsubmit="register()" autocomplete="off">
...
</form>

我知道这种形式的每个孩子都在工作。 然后,在下面的<script>标记中,有我的主要函数,当提交上述表单时会调用该函数。而且我知道register函数之外的所有东西都在运行。但是,当我在表单的每个字段中输入随机值并按Submit时,控制台显示在表单的register()属性中调用的onsubmit函数不存在。我似乎在这里找不到问题:

//Global Vars
var firebaseConfig = { ...
};
firebase.initializeApp(firebaseConfig);
var db = firebase.firestore();
var registerButton = document.querySelector("#registerButton");
//Main Register Function
function register() {
  event.preventDefault();
  //Locally Global Variables
  var fullName = document.forms["register"]["fullName"].value;
  var username = document.forms["register"]["username"].value.toLowerCase();
  //The MD5 is a way to hash the password, that way the real password is safe and only the hash is used
  var password = md5(document.forms["register"]["password"].value);
  var serviceProvider = document.forms["register"]["serviceProvider"].value;
  //Simple If Statement that adds appropriate email suffix based on Service Provider
  if (serviceProvider === "Verizon") {
    serviceProvider = "@vtext.com";
  } else if (serviceProvider === "ATT") {
    serviceProvider = "@txt.att.net";
  } else if (serviceProvider === "TMobile") {
    serviceProvider = "@tmomail.net";
  } else if (serviceProvider === "Sprint") {
    serviceProvider = "@messaging.sprintpcs.com";
  }
  var phoneNumber = document.forms["register"]["phoneNumber"].value + serviceProvider;
  var emailAddress = document.forms["register"]["emailAddress"].value;
  //Checks The Database If The Username Is Already Taken Or Not
  db.collection("Users").where("username", "==", username).get()
    .then(function(querySnapshot) {
      //Checks Each Individual Result -- If there are no results, than this code will not run
      try {
        querySnapshot.forEach(function(doc) {
          //If any result exists, stop here
          if (doc.data()) {
            alert("I'm sorry but this username is already taken!! Please Try Another One");
            throw "Error";
          }
        });
      } catch (error) {
        if (error === "Error") {
          return;
        }
      }
      //If not
      //Add All Of The User Info To The Database
      db.collection("Users").doc(username).set({
          fullName: fullName,
          username: username,
          password: password,
          phoneNumber: phoneNumber,
          emailAddress: emailAddress,
          chatsInvolvedIn: []
        })
        .then(function() {
          //If it succeeds, give user the heads up and then take them to their new homepage
          alert("Your account under the username " + username + " has been sucessfully created. You will now be redirected to your homepage.");
          //Place Code Underneath to Handle Keeping user Logged In For Present and Future Visits, along with redirecting to a homepage
          //Code Goes Here
          db.collection("Users").doc(username).get().then(function(doc) {
            if (doc.exists) {
              localStorage.setItem("loggedIn", JSON.stringify(doc.data()));
            }
            alert(localStorage.getItem("loggedIn"));
            //window.location.replace("index.html");
          });

        })
        .catch(function(error) {
          //If it fails, tell user to try again later (we don't care about the error message during production, because it is unlikely after our many tests)
          alert("I'm sorry but your account was not successfully created due to an unexpected error. Please try again later.");
        });

    })
    .catch(function(error) {
      //If checking the database originally for duplicate usernames fails, then give the user the same warning as above
      alert("I'm sorry but your account was not successfully created due to an unexpected error. Please try again later.");
    });
}

我知道我上面的编程实践不是最好的。如果您能帮助我,那就太好了,谢谢!!

0 个答案:

没有答案