【问题标题】:How to fetch one document from Firebase and how to pass the id to delete it?如何从 Firebase 获取一个文档以及如何传递 id 来删除它?
【发布时间】:2019-12-12 21:04:14
【问题描述】:

这是我的 react native + firebase 项目,我有 2 个问题:

  1. 你建议如何传递一份简历的 id ?

  2. 我如何从 firebase 只获取一份简历,因为如果我尝试这样做,它会给我这个错误:

    TypeError: undefined is not an object (evalating 'querySnapshot.docs.map')]

从集合中获取所有文档很好

getCv: () => {
  const id = "eccc137b-88be-470d-a0b8-c90b58a6473a"
  return firebase
      .firestore()
      .collection('cvs')
      .doc(id)
      .get()
      .then(function(querySnapshot) {
        let cvs = querySnapshot.docs.map(doc => doc.data())
        // console.log(doc.data())
        return cvs
      })
      .catch(function(error) {
        console.log('Error getting documents: ', error)
      })
}

这是我的 fetchCV 方法

  fetchCvs = async () => {
    try {
      const cvs = await this.props.firebase.getCv()
      //const cvs = await this.props.firebase.getCvs()
      //console.log(cvs)
      this.setState({ DATA: cvs, isRefreshing: false })
    } catch (e) {
      console.error(e)
    }
  }

这就是我添加一份简历的方式

onSubmit = async () => {
        try {
          const cv = {
            photo: this.state.image,
            title: this.state.title,
            description: this.state.description,
            salary: this.state.salary,
            createdAt: new Date().toISOString()
          }
          this.props.firebase.uploadCv(cv)

          this.setState({
            image: null,
            title: '',
            description: '',
            salary: '',
            createdAt: ''
          })
        } catch (e) {
          console.error(e)
        }
      }
uploadCv: cv => {
        const id = uuid.v4()
        const uploadData = {
        id: id,
        cvPhoto: cv.photo,
        cvTitle: cv.title,
        cvDescription: cv.description,
        cvSalary: cv.salary,
        cvCreatedAt: cv.createdAt
        }
        return firebase
        .firestore()
        .collection('cvs')
        .doc(id)
        .set(uploadData)
    },

这就是我实现 deleteCv 方法的方式

onDelete = async () => {
        const cvId = {
          id: this.state.title
        }
        //this.props.firebase.deleteItem(cv);
        const deleteId = this.props.firebase.deleteItem(cv);
        console.log(deleteId)
      }

【问题讨论】:

    标签: reactjs firebase react-native google-cloud-firestore


    【解决方案1】:

    当我在 nodejs 中尝试类似的代码时,我遇到了不同的错误,但我认为这是相同的原因。排队:

    let cvs = querySnapshot.docs.map(doc => doc.data())

    当您在 DocumentReference 上使用 get 时,querySnapshot 是 DocumentSnapshot 的实例,它没有属性 docs。我认为您应该先使用querySnapshot.data(),然后再操作返回的数据。

    或者您可能想在集合上使用get,而不是在文档上,然后您将获得QuerySnapshot 对象和.doc 数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 2021-10-26
      • 2020-11-02
      • 2019-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多