【问题标题】:React Query Firestore DataReact 查询 Firestore 数据
【发布时间】:2020-08-14 09:45:22
【问题描述】:

我正在尝试查询我的 redux 存储中的数据以从 firebase 获取数据,但我得到未定义 我哪里错了?请有人帮助我。是我查询数据的方式不对还是?

房子班:

class House {
    constructor( 
        id,
        name,
        imageUri,
        method
    ){
        this.id = id;
        this.name = name;
        this.imageUri = imageUri
        this.method = method;
    
    }
    
}

export default House

Redux 商店:

import firebase from 'firebase'
import House from '../../models/House'

const initialState = {
    favourites :[],
}
const FavsReducer = (state=initialState, action) =>{
    switch(action.type){
        case TOGGLE_FAVOURITE:
                let myfav = new House()
                firebase.firestore()
                    .collection('Posts')
                    .where('postId', '==', action.houseId)
                    .get()
                    .then(snapshots => {
                        snapshots.docs.forEach(doc=>{
                            myfav = new House(
                                doc.data().id,
                                doc.data().name,
                                doc.data().image,
                                doc.data().method
                            )
                        })
                    })
                return {...state, favourites: state.favourites.concat(house)}
        default: 
            return state
    }
}
export default FavsReducer

当我console.logfavourites 数组时

Array [
  House {
    "id": undefined,
    "imageUri": undefined,
    "method": undefined,
    "name": undefined,
  },
]

火炉

【问题讨论】:

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


    【解决方案1】:

    怎么样:

    firebase.firestore()
            .collection('Posts')
            .doc(action.houseId)
            .get()
            .then((docRef) => { console.log(docRef.data()) })
    

    【讨论】:

    • 非常感谢您,您的回答帮了您的命。那行得通。上帝保佑你。
    • 还有一件事,。如何将docRef.data() 添加到新数组中
    • 您可以在查询之外声明一个变量,例如 var arr=[] 并在 then 函数中使用 arr.push(docRef.data())。我不知道我是否明白你的意思
    • 好的,我通过连接正确或将它返回到我的收藏夹数组的方式是?
    • 因为这是一个查询所以它不会立即返回值,想象一下它就像一个超时。你可以看看这个链接stackoverflow.com/questions/56126566/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多