ColeB
ColeB

Reputation: 31

Firebase Database unique id per save

My code below works PERFECT, but I have one issue.

        set(ref(db, "People/"+ ?????),{
            Email: email,    
            Password: password,
            })
            .then(()=>{
                console.log('Success');
            })
            .catch((error)=>{
                alert(error);
            });
        delay(1000).then(() => window.location.href='https://thecoletimes.ml/home');
      function delay(time) {
  return new Promise(resolve => setTimeout(resolve, time));
}
    })
    .catch((error) => {
        const errorCode = error.code;
        const errorMessage = error.message;
        alert('Your request was rejected because either your email is already in use, or your email is invalid. Please double check your info.');
    });

When I am saving the data, I want each person to have a unique id as the string.

I have though about math.random

But then if people got the same number, they would have the same number. I need each person to have a unique string.

I am using html and javascript.

Upvotes: 2

Views: 117

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

If you want to generate a unique ID, it's recommended to use Firestore's built-in addDoc function instead of setDoc:

addDoc(ref(db, "People"),{
    Email: email,    
    Password: password,
    })
    .then(()=>{
        console.log('Success');
    })
    .catch((error)=>{
        alert(error);
    });

Also see the Firebase documentation on adding a document.

Upvotes: 1

Related Questions