使用Firebase删除数据后,为什么我无法重新添加相同名称的数据?

时间:2018-05-29 19:24:55

标签: javascript database reactjs firebase firebase-realtime-database

在我的应用中,用户可以添加亲人,如下所示:

  addLovedOne(event) {
    const {
      lovedOne,
    } = this.state;

    const unsubscribe = firebase.auth().onAuthStateChanged((user) => {
      if (user) {
        db.addLovedOne(user.uid, lovedOne)
          .then(() => {
            this.setState({ lovedOne: '' });
            this.refreshLovedOnes();
          })
          .catch(error => {
            this.setState(byPropKey('error', error));
          });
      } else {
        unsubscribe();
      }
    });

    event.preventDefault();
  }

当然,用户可以删除亲人,例如:

  removeLovedOne(event) {
    const lovedOne = event.target.id;

    const unsubscribe = firebase.auth().onAuthStateChanged((user) => {
      if (user) {
        const ref = firebase.database().ref(`users/${user.uid}/lovedOnes`);
        const removeLovedOne = ref.orderByChild('lovedOne').equalTo(lovedOne);

        removeLovedOne.on('value', (snapshot) => {
          const lovedOneId = snapshot.node_.children_.root_.key;

          db.removeLovedOne(user.uid, lovedOneId, lovedOne)
            .then(() => {
              this.refreshLovedOnes();
            })
            .catch(error => {
              this.setState(byPropKey('error', error));
            });
        });
      } else {
        unsubscribe();
      }
    });
  }

以下是查询的内容:

export const addLovedOne = (userId, lovedOne) => (
  db.ref(`users/${userId}/lovedOnes`).push({
    lovedOne,
  })
);

export const removeLovedOne = (userId, lovedOneKey) => (
  db.ref(`users/${userId}/lovedOnes/${lovedOneKey}/lovedOne`).remove()
);

这是我的架构的样子: enter image description here

知道为什么我在删除页面后无法添加具有相同名称的亲人而不刷新页面?

0 个答案:

没有答案