简体   繁体   中英

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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