firebase cloud firestore中某个领域的CRUD子项的最佳方法是什么?

IT技术 javascript arrays reactjs firebase google-cloud-firestore
2022-07-19 01:32:56

我一直被困在用户和项目之间创建多对多关系。很多人说 array 比 map 好,但我真的不知道如何开始。那么哪个更适合这个呢?我无法提交给 projNo 的孩子,也无法从集合“项目”中选择项目。下拉菜单显示“项目”集合中的 projNo,但我无法将其更改为新值。

import React, { Component } from 'react';
import fire from '../config/Fire';
import { Link } from 'react-router-dom';
import Navigation from '../components/Navigation';

class EditUser extends Component {

  constructor(props) {
    super(props);
    this.unsubscribe = null;
    this.state = {
      key: '',
      authority: '',
      name: '',
      email: '',
      password: '',
      projNo: {projNo1: '', projNo2: '', projNo3: ''},
      project: []
    };
  }

  onCollectionUpdate = (querySnapshot) => {
    const project = [];
    querySnapshot.forEach((doc) => {
      const { projNo } = doc.data();
      project.push({
        key: doc.id,
        doc, // DocumentSnapshot
        projNo
      });
    });
    this.setState({
      project
    });
  }

  componentDidMount() {
    const ref = fire.firestore().collection('user').doc(this.props.match.params.id);
    ref.get().then((doc) => {
      if (doc.exists) {
        const user = doc.data();
        this.setState({
          key: doc.id,
          authority: user.authority,
          name: user.name,
          email: user.email,
          password: user.password,
          projNo: user.projNo
        });
      } else {
        console.log("No such document!");
      }
    });

    this.unsubscribe = fire.firestore().collection('project').onSnapshot(this.onCollectionUpdate);
  }

  onChange = (e) => {
    const state = this.state
    state[e.target.name] = e.target.value;
    this.setState({user:state});
  }

  onSubmit = (e) => {
    e.preventDefault();

    const { authority, name, email, password, projNo, projNo1, projNo2, projNo3 } = this.state;

    const updateRef = fire.firestore().collection('user').doc(this.state.key);
    updateRef.set({
      authority,
      name,
      email,
      password,
      projNo,
      projNo1,
      projNo2,
      projNo3
    }).then((docRef) => {
      this.setState({
        key: '',
        authority: '',
        name: '',
        email: '',
        password: '',
        projNo: { projNo1: '', projNo2: '', projNo3: '' }
      });
      this.props.history.push("/show/"+this.props.match.params.id)
    })
    .catch((error) => {
      console.error("Error adding document: ", error);
    });
  }

  render() {
    return (
    <div>
       ...
    </div>
    );
  }
}

export default EditUser;

数据库的图像

1个回答

如果您只想显示用户连接到一组项目,那么我会切换到一个数组。

要在 firestore 的数组中添加和删除项目,您可以在此处参考 firebase 文档:https ://firebase.google.com/docs/firestore/manage-data/add-data?authuser=0#update_elements_in_an_array

因此,当您创建用户时,只需切换到设置数组,如下所示:

updateRef.set({
          authority,
          name,
          email,
          password,
          projNo: [projNo1,projNo2,projNo3]
        })

将来,如果您想从projNo数组中原子地添加或删除项目,可以这样实现:

// Add
updateRef.update({
    projNo: firebase.firestore.FieldValue.arrayUnion("projNo16")
});

// Remove 
updateRef.update({
    projNo: firebase.firestore.FieldValue.arrayRemove("projNo1")
});

请记住,您需要导入firebase到调用上述内容的文件中,否则您将无法使用 FieldValue 方法。