在Firebase Cloud Firestore中对某个领域的孩子进行CRUD的最佳方法是什么?

时间:2019-03-05 02:26:18

标签: javascript arrays reactjs firebase google-cloud-firestore

我一直被困在创建用户与项目之间的多对多关系上。许多人告诉我们数组比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;

Image for the database

1 个答案:

答案 0 :(得分: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方法。