【问题标题】:How do you add data to firebase doc using redux thunk getFireBase and getFirestore from react-redux-firebase如何使用来自 react-redux-firebase 的 redux thunk getFireBase 和 getFirestore 将数据添加到 firebase doc
【发布时间】:2022-01-23 11:59:11
【问题描述】:

问题

  • 我在使用 react-redux-thunk 向 firebase 数据库添加数据时遇到问题
  • 我检查了文档并观看了多个 youtube 视频,但在出错后一直出错
  • 我想分别使用“r​​edux-firestore”和“react-redux-firebase”库中的getFirestoregetFirebase 中间件将数据添加到firebase 文档

代码

  • 这里是动作函数
  • 所有代码似乎都在工作,但我不知道如何将 validatonResults 添加到 users/uid(请参阅下面代码中的注释)
  • 尝试了以下代码(以及许多其他内容),但没有成功firebase.push('users', { data: validatonResults })
import inputValidationAPI from "../../apis/inputValidationAPI"

export const validateTestCase = payload => {
    return (dispatch, getState, { getFirebase, getFirestore }) => {
        const fireStore = getFirestore()
        const firebase = getFirebase()
        const uid = getState().firebase.auth.uid

        inputValidationAPI
            .post("/submitTerm", {
                term: payload
            })
            .then(response => {
                return response.data.data
            })
            .then(validatonResults => {
                /* 
                validationResults = ["string1", "string2", "string3"]
                I want to take validationResults and add them into firebase collection something like:
                
                firebase.push('todos/${uid}', { some: 'data' }) 
                */

                return validatonResults
            })
            .then(validatonResults => {
                dispatch({
                    type: "VALIDATE_TESTCASE_SUCCESS",
                    payload: validatonResults
                })
            })
            .catch(err => console.log(err))
    }
}

【问题讨论】:

  • 在向 firebase 数据库添加数据时,您遇到了什么问题?你能在这里显示你的错误吗?
  • 想通了,关闭这个问题
  • @OG_On_The_Beat 你发现了什么?你能把它作为答案发布吗?当有人遇到同样的问题时,它会帮助社区。​​span>

标签: javascript firebase google-cloud-firestore redux react-redux-firebase


【解决方案1】:

以下语法解决了这个问题:

fireStore.update(
          { collection: "users", doc: uid },
          { dataToSave: someJSONObject },
     }
)

【讨论】:

    猜你喜欢
    • 2021-08-11
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 2021-03-14
    • 1970-01-01
    相关资源
    最近更新 更多