使用JavaScript对Firebase数据库中的电子邮件ID和电话号码进行重复检查

时间:2019-05-16 07:06:11

标签: javascript jquery firebase firebase-realtime-database

我是Firebase的新手。我正在为我的应用程序创建一个基本的联系表,并将其连接到firebase数据库。我的应用程序没有任何身份验证。 。我已将所有规则设置为True。我想防止重复,如果数据库中存在电话号码和电子邮件,我想显示一条错误消息。

下面是我尝试过的代码

firebase.initializeApp(firebaseConfig);
// Reference messages collection

// Listen for form submit
document.getElementById('contactform1').addEventListener('submit', submitForm);
// Submit form
function submitForm(e) {
  e.preventDefault();
  // Get values
  var fname = getInputVal('fname');
  var lname = getInputVal('lname');
  var email = getInputVal('email');
  var phone = getInputVal('phone');
  var skills = getInputVal('skills');
  var jobId = getInputVal('jid');
  var linkedin = getInputVal('linkedin');
  var github = getInputVal('github');
  var location = getInputVal('location');


  // Save message
  saveMessage(fname, lname, email, skills, phone, jobId, linkedin, github, location, );
  // file upload
  var fileButton = document.getElementById("fileButton");
  var file = fileButton.files[0];

  firebase.storage().ref('self/' + file.name).put(file);
  
}
// Function to get get form values
function getInputVal(id) {
  return document.getElementById(id).value;
}

// Save message to firebase
function saveMessage(fname, lname, email, skills, phone, jobId, linkedin, github, location) {
  firebase.database().ref().child('self/data/' + phone).set({
    name: fname + " " + lname,
    email: email,
    phone: phone,
    skills: skills,
    jobId: jobId,
    linkedin: linkedin,
    github: github,
    location: location
  });
}
//snapshot to check the values in database
  firebase.database().ref().child('self/data/' ).on('child_added', snap => {
    var name = snap.child('name').val();
    var email = snap.child('email').val();
    var phone = snap.child('phone').val();
    var skills = snap.child('skills').val();
    var jobId = snap.child('jobId').val();
    var linkedin = snap.child('linkedin').val();
    var github = snap.child('github').val();
    var location = snap.child('location').val();
   
    $('#table_bdy').append('<tr><td>' + name + '</td><td>' + email + '</td><td>' + phone + '</td><td>' + skills + '</td><td>' + jobId + '</td><td>' + location + '</td><td>' + github + '</td><td>' + linkedin + '</td></tr>')

  })

1 个答案:

答案 0 :(得分:1)

在将新值保存到数据库之前,请从数据库中检索数据,并检查以表格形式编写的电子邮件是否与从数据库中检索的电子邮件相同。例如:

  var newEmail = getInputVal('email');
  firebase.database().ref().child('self/data/' ).on('child_added', snap => {
  var name = snap.child('name').val();
  var email = snap.child('email').val();
  if(newEmail.trim() === email.trim())
  {
     console.log("email already exists in the database");
  }
  else 
  {
    saveMessage(fname, lname, newEmail, skills, phone, jobId, linkedin, github, location);
  }
});

尝试以下操作:

firebase.initializeApp(firebaseConfig); 
 // Reference messages collection 

// Listen for form submit 
document.getElementById('contactform1').addEventListener('submit', submitForm); 
// Submit form 
function submitForm(e) { 
  e.preventDefault(); 
// Get values 
 var fname = getInputVal('fname'); 
 var lname = getInputVal('lname'); 
 var newEmail = getInputVal('email'); 
 var newPhone = getInputVal('phone'); 
 var newskills = getInputVal('skills'); 
 var newjobId = getInputVal('jid'); 
 var newlinkedin = getInputVal('linkedin'); 
 var newgithub = getInputVal('github'); 
 var newlocation = getInputVal('location'); 

saveMessage(fname, lname, newEmail, newskills, newPhone, newjobId, newlinkedin, newgithub, newlocation); 
} 

function saveMessage(fname, lname, newEmail, skills, phone, jobId, linkedin, github, location) { 
 let ref = firebase.database().ref().child('self/data/'); 
  ref.on('value', snap => { 
   if(snap.exists()) 
   { 
     snap.forEach(childSnapshot => { 
     var name = childSnapshot.child('name').val(); 
     var email = childSnapshot.child('email').val(); 
     var phone = childSnapshot.child('phone').val(); 
     var skills = childSnapshot.child('skills').val(); 
     var jobId = childSnapshot.child('jobId').val(); 
     var linkedin = childSnapshot.child('linkedin').val(); 
     var github = childSnapshot.child('github').val(); 
     var location = childSnapshot.child('location').val(); 
     var status = childSnapshot.child('status').val(); 
     console.log(status); 
      if (newEmail.trim() === email.trim()) 
      { //check if email exists 
        console.log("email already exists in the database"); 
      } 
      else 
      { 
         console.log('hello'); 
         firebase.database().ref().child('self/data/' + phone).set({ 
         name: fname + " " + lname, 
         email: email, 
         phone: phone, 
         skills: skills, 
         jobId: jobId, 
         linkedin: linkedin, 
         github: github, 
         location: location 
       }); 
      } 
   }); 
  } 
  else 
  {  
    firebase.database().ref().child('self/data/' + phone).set({ 
    name: fname + " " + lname, 
    email: email, 
    phone: phone, 
    skills: skills, 
    jobId: jobId, 
    linkedin: linkedin, 
    github: github, 
    location: location 
      }); 
    } 
  }); 
 } 

// Function to get get form values 
function getInputVal(id) { 
   return document.getElementById(id).value; 
}

首先,您需要从表单中检索值,然后在方法中调用方法saveMessage,以检查节点self/data是否存在,然后检索数据并检查数据库中是否已存在电子邮件。