繁体   English   中英

Firestore - 如何在删除他的帐户时从他自己创建的所有文档中删除用户 ID?

[英]Firestore - how do I remove user's ID from all the documents he's created himself when deleting his account?

我的应用处理用户为自己创建简单的预订,以便他们可以跟踪它们,因此当在 Firestore 中创建预订时,它有一系列用户进行相同的预订(意思是相同的姓名、日期和放映时间)。 我 go 如何从提到的所有 arrays 中删除用户 ID,并在删除他的帐户时将其他用户 ID 留在其中?

这是我的代码:

const ProfileScreen = () => {

  const navigation = useNavigation();
  const auth = getAuth();
  const uid = auth.currentUser.uid;
  const docRef = doc(db, 'users', uid);

  const handleSignOut = () => {
    auth
    .signOut()
    .then(() => {
        navigation.navigate('Welcome')
    })
        .catch(error => alert(error.message))
  }

  const handleDeleteProfile =() => {    
    const korisnikID = getAuth().currentUser.uid;
    const user = getAuth().currentUser;

    auth.signOut()
    .then(() => {
      navigation.navigate('Welcome');
      // removing the user ID from the arrays
      deleteDoc(doc(db, "users", korisnikID))
      .then(() => {        
        deleteUser(user);
      }).catch((error) => {
        console.log(error);
      });
    });  
  }

  const [user, setUser] = useState({});

  useEffect(() => {
    const getUser = async () => {
      const snap = await getDoc(docRef)
      setUser({user, ...snap.data()})
    }
    getUser()
  },[])

  return (
<View style = {styles.container}>   
  <View style = {styles.profileContainer}>
  <Image source={require('../assets/profile.jpeg')} style = {styles.image} />
  <View style = {styles.profileInfo}>
  <Text style = {styles.name}>{user.name} ({user.age})</Text>
  <Text style = {styles.location}>{user.city}, {user.country}</Text>
  </View>
  </View>

  <View style = {styles.infoContainer}>
  <Text style = {styles.text1}>Username</Text>
    <View style = {styles.info}>
      <Text style = {styles.text2}>{user.username}</Text>
    </View>
    <Text style = {styles.text1}>Favorite movie:</Text>
    <View style = {styles.info}>
      <Text style = {styles.text2}>{user.movie}</Text>
    </View>
    <Text style = {styles.text1}>About me</Text>
    <View style = {styles.info}>
      <Text style = {styles.text2}>{user.bio}</Text>
    </View>
  </View>
  <View style = {styles.buttonContainer}>
    <TouchableOpacity onPress = {handleSignOut}
    style = {styles.button}>
      <Text style = {styles.buttonText}>Sign out</Text>
    </TouchableOpacity>
  </View>
  <View style = {styles.buttonContainer}>
    <TouchableOpacity onPress = {handleDeleteProfile}
    style = {styles.buttonDelete}>
      <Text style = {styles.buttonText}>Delete account</Text>
    </TouchableOpacity>
  </View>
</View>
 )
}

export default ProfileScreen

这是 Firestore 数据库的样子:

用户字段

预订字段

将以下代码放入循环中以删除特定的 id 此没有循环的代码将删除第一次出现的用户 id。 {put the user id you want to remove} 应该替换为enter image description here the id here mentioned here。

以下代码是针对用户db

var postsRef = db.collection('users');
var query = postsRef.where('id', '==', '{put the user id you want to remove}').get()
    .then(snapshot => {
      snapshot.forEach(doc => {
        console.log(doc.id, '=>', doc.data());
        var deleteDoc = db.collection('users').doc(doc.id).delete();
      });
    })
    .catch(err => {
      console.log('Error getting documents', err);
    });

以下代码用于预订数据库

var postsRef = db.collection('booking');
var query = postsRef.where('id', '==', '{put the user id you want to remove}').get()
    .then(snapshot => {
      snapshot.forEach(doc => {
        console.log(doc.id, '=>', doc.data());
        var deleteDoc = db.collection('booking').doc(doc.id).delete();
      });
    })
    .catch(err => {
      console.log('Error getting documents', err);
    });

以下代码用于电影数据库

var postsRef = db.collection('movies');
var query = postsRef.where('id', '==', '{put the user id you want to remove}').get()
    .then(snapshot => {
      snapshot.forEach(doc => {
        console.log(doc.id, '=>', doc.data());
        var deleteDoc = db.collection('movies').doc(doc.id).delete();
      });
    })
    .catch(err => {
      console.log('Error getting documents', err);
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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