在Firebase Cloud Firestore中对某个领域的孩子进行CRUD的最佳方法是什么? [英] What is the best way to CRUD children of a field in firebase cloud firestore?

查看:45
本文介绍了在Firebase Cloud Firestore中对某个领域的孩子进行CRUD的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直被困在创建用户与项目之间的多对多关系上。许多人说数组比地图好,但我真的不知道如何开始。那么哪个更适合这个呢?我无法提交给projNo的孩子,也无法从项目集合中选择该项目。下拉列表显示项目集合中的projNo,但是我无法将其更改为新值。

I have been stuck forever to create many to many relation between user and project. Many people told that array is better than map but I don't really know how to start. So which is better for this one ? I can't submit into projNo's child and can't select the project from the collection "project". The drop-down shows the projNo from "project" collection but I can't change it to a new value.

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;

数据库图像

推荐答案

如果您只想显示用户是连接到一组项目,然后切换到数组。

If you just want to show that a user is connected to a set of projects then I would switch to an array.

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

To add and remove projects from an array in firestore you can refer to the firebase docs here: https://firebase.google.com/docs/firestore/manage-data/add-data?authuser=0#update_elements_in_an_array

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

So when you create the user just switch to settings an array, like so:

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

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

In the future, if you want to atomically add or remove projects from the projNo array, this can be achieved like so:

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

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

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

Remember, you will need to import firebase into the file where you are calling the above otherwise you can't use FieldValue methods.

这篇关于在Firebase Cloud Firestore中对某个领域的孩子进行CRUD的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆