last day (16 days later) » 

9:10 AM
0
A: Duplication check of Email id and Phone number in Firebase database using JavaScript

Peter HaddadBefore saving the new values to the database, retrieve the data from the database and check if the email written in the form is the same as an email retrieved from the database. For example: var newEmail = getInputVal('email'); firebase.database().ref().child('self/data/' ).on('child_added',...

 
Sai
How to get the newEmail value without submitting the form? if i use set time out function i can get the value. How can i fix it out?
 
inside your function function submitForm(e) { you are calling saveMessage() and you are also retrieving the form values. So, instead of immediately calling saveMessage() add my code that I gave you in the answer
 
Sai
It didn't work out. Please have a look at my code codepen.io/manojsai/pen/MdpPqW . Actually i'm trying to snap the values on page load
 
add the if condition before creating the table, and in your code, you are retrieving the data inside the eventclicklistener function... so when you click submit you are retrieving...
 
Sai
Can you please edit the snippet. I'm little confused. I tried that too . Now i'm not even able to send the data to database
 
Sai
Didn't work
 
whats not working?
are you adding console.log to check the values?
 
Sai
yes
im not getting anything in console
 
where are u adding console.log
 
Sai
im trying to print hello after submitting the form
even that im not able to get
I tried to print the values of input fileds
 
9:14 AM
add console.log("email", newEmail);
after this
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');
and tell me what u get
 
Sai
there i'm getting the email and all the values
 
Great
Immediately inside onchildadded
Write `console.log(name);
After getting the name inside on chiladded and tell me what u get
 
Sai
i'm not getting anything over there
actually we don't have any values as of now in the database.
so it will be empty
even i'm not getting empty string
 
what do u have in the database?
send me a screenshot
if you dont have self/data in the database then yes it wont work
 
Sai
nothing is there as of now
 
9:23 AM
u need to add data to the database to check if there is duplication.............
 
Sai
but if i start this application as a start . first i need to enter some values to database to check them
So how can i handle situaton
situation
 
console.log(name); what are u getting here
undefined?
 
Sai
nothing as of now
now i added one item to database
now if i try it is throwing me this error
Reference.child failed: First argument was an invalid path = "self/data/[object HTMLInputElement]". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
 
what did u add after data?
 
Sai
this is the data
 
9:41 AM
Are u adding a, new phone?
 
Sai
yes, my contact have all these
i'm adding phone number too
 
What is the new phone u are adding
 
Sai
var newPhone = getInputVal('phone');
 
I mean the value
 
Sai
8888888888
some odd numbers length of 10
should i create a new firebase and o i send you the credentials?
*should i send
 
10:09 AM
are u using visual studio code?
try this
removing everything from the database and try it
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');
 
Sai
yes
vs code
 
Sai
downloaded
 
okay try the code
 
Sai
will ty noe
 
10:17 AM
remove everything from the database and try it
 
Sai
ok
 
copy paste it
 
Sai
newEmail is not defined
fixed that
now getting console.log value
again its not showing up
im not able to add
even i'm not getting any error in console
 
which console.log are u getting?
 
Sai
nothing
i didnt get anything if i removed everything from database
 
11:20 AM
Add a log inside if(snap.exists())
To check if it enters there
 
Sai
where should i add
after else?
ok got it
 
Inside if(snap.exists()){ console.log("isnide snap") ;
 
Sai
but there is no snap as of now
database is clean
 
If there is nothing in the database then it shouldn't enter inside if(snap.exists()
It should go to the else
Yes just testing
Is it printing the console.log in the else?
 
Sai
nope
it's not printing
even in if i'm not getting anything
but everything is written in if condition
 
11:27 AM
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');
copy paste
 
Sai
not getting any value
saveMessage(fname, lname, newEmail, newskills, newPhone, newjobId, newlinkedin, newgithub, newlocation);
function saveMessage(fname, lname, email, skills, phone, jobId, linkedin, github, location)
 
Sai
in second one only lname and fname are defined i guess
email, skills, phone, jobId, linkedin, github, location doesnt hold any value
 
Add a console.log immediately under saveMessage
console.log(email);
 
Sai
ok
yep, i'm getting the wmail
email
 
11:36 AM
Then the values are inside the method savemessage
 
Sai
yes
 
OK what's the problem
Did u check if are entering the else
 
Sai
not entering
checked
 
Change console.log('hello'); to console.log('goodbye'); in the second if
 
Sai
nope
 
11:38 AM
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');
 
Sai
not entering
 
copy paste
 
Sai
not entering
 
It's not entering the if and not entering the else
It's magic!
 
Sai
yeah
not even if nor else
 
11:40 AM
Magic I tell u!!!
 
Sai
then how can i find that out :D
 
Its magic I tell u
We cannot beat it
 
Sai
hmmm
from three days i'm trying this out
 
So it enters savemessage and the magically doesn't enter neither if nor else
 
Sai
ya
 
11:43 AM
Coding is magic
 
Sai
i'm still learning that. newbie
so i'm trying and learning
 
Yes u need to say the magic spell for it to worj
Work
 
Sai
magic :D
 
U probably have written something wrong
There is no way it enters savemessage and doesn't enter neither fi nor else
 
Sai
I have shown you my complete code. and even i have set all the rules to true in my firebase console
its entering if condition if there exists data in firebase
 
11:47 AM
If no data exists then it should enter else
 
Sai
that's what i'm not understanding
see now it entered the if condition and showed me console value
if i add data in database
and now i got hello too
but there should be some data in database
 
Are u using the same code I gave u
 
Sai
small tweek i have done. it showed me newEmail not defined
i have added newEmail instead of email in function savemEssage
saveMessage
 
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');
just put a console.log in the else
and check
copy paste
 
Sai
ok will check it now
should i remove everything from database
 
11:55 AM
yes
to check if it enters the else
so remove the self/data node
 
Sai
newEmail is not defined
 
yes fix that
 
Sai
yes
 
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');
copy paste
 
Sai
should i remove data from database
 
11:57 AM
yes remove data under self/data
 
Sai
ok
 
remove the self node from the database
 
Sai
not getting anything
 
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');
copy paste
100% working
 
Sai
nope
 
12:02 PM
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');
copy paste
and tell me what u get
 
Sai
didn't get into the condition
got nothing
 
send ma screenshot of ur database
 
Sai
ok
 
Sai
no branches
its clean and empty
 
12:07 PM
it didnt enter nor if nor else?
send a screenshot
 
Sai
sending
 
what is this magic!!
 
Sai
 
1
Q: Check if username exists in firebase database in javascript

JeanI use Firebase Function to make a function that allows to complete a user profile by creating a directory in the Firebase Realtime Database like this : { users: { AeknQrtMIyPpC4EQDPNQYvQUxCA3: { username : test profilePicture : https://url.com/1 } Aek...

0
Q: How to check if data exist?

Isaac LemFirebase rules configuration: { "rules": { ".read": "auth != null", ".write": "auth != null" } } Firebase database data sample Using this idea, I came up with something as below but not working. const { currentUser } = firebase.auth(); console.log(currentUser.uid); firebase....

 
Sai
im not using auth
will check those
 
12:11 PM
they are the same
try this
copy paste
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');
delete database and tell me if it shows the log
100% should work
 
Sai
it worked
no data in databse
 
i told u 100%
should work
we won!!
against the magic!!
 
Sai
instead of child we need to change it as value !!
yeah we won :D
 
yes
but now u need to loop
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');
copy paste
and tell me what u get
 
Sai
no data in database
and this error
 
12:20 PM
What error
 
Sai
cannot read the property of null
null property error
 
Which property
Copy paste the error
Or send me a screenshot
 
Sai
 
9oay
Okay
Add before the if
console.log(status);
Have u added anything to the database
 
Sai
yes got added
deleted that now
 
12:25 PM
how did u add it
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');
copy paste
 
Sai
by the code itself
 
tell me if u get this console.log(status);
 
Sai
it got added
 
when adding
ok the error says cannot read property 'trim' of null
so either email retrieved is null
or email in savemessage is null
 
Sai
status null
'no data in databaseeeeeeeeeeeeeeeeee' is also getting in
data was also added in database
 
12:29 PM
send me a screenshot of ur database
with data
the data that u are adding through code
 
Sai
email is not getting added there
 
Sai
12:56 PM
the email value is not passing the conditions
I HAVE FIXED THAT
but till reset the form the values are being sent to database
like if i delete the data in db, still it gets added untill i refresh the form
 
1:11 PM
sorry was eating
good job!
please upvote my answer and mark it as correct!
 
Sai
yeah, but the loop is getting iterated n number of times
 
change this
ref.on('value', snap => {
into ref.once('value', snap => {
 
Sai
and when i add the values, the values are getting over written with new values
new branch is not getting added
 
because u need to add a new phone
not same phone
if u add a new phone, u will get another branch
with data
 
Sai
yeah perfect
please edit the answer
i will accept and upvote
thankyou very much
I have learnt a lot
if i wanna check duplication of both phone number and email similarly do i need to add and condition?
like if i applied with email and same phone number for the 2nd time it should throw error
same email and same phone > error
 
1:21 PM
Yes
And && in the if
 
Sai
if (newEmail.trim() === email.trim() && newPhone.trim()== phone.trim())
 
Sai
tq
can i upvote and accept answer directly or will you update there?
 
yes upvote and mark as correct
im updating it now, but u can upvote and mark it as correct
 
Sai
ok
if you feel my question was good please upvote and protect it. Tx
 
1:30 PM
updated
 
Sai
yeah accepted your answer
but one query left over
if (newEmail.trim() === email.trim() || newPhone.trim()== phone.trim())
like if either or exists im throwing error
but some times its getting added to database
 
what error
>but some times its getting added to database
because it is or condition
so if the email doesnt exists in the database but the phone does exists then it will enter the condition
 
Sai
if any of them exists in data base i dont want to add them in data base
how can i make this condition
so do i need to write or condition and and condition
 
only and
 
Sai
I used and condition but still it got added
so i'm trying to change
both emails are same but still got added with different number
and on singe submit it is getting added twice
 
2:00 PM
What different number?
 
Sai
see the two email id are same but still they got added with different phone numbers
i used &&
 

  last day (16 days later) »