简体   繁体   English

如何从 Cloud Firestore 获取数据然后对其进行过滤、映射然后返回?

[英]How to get data from cloud firestore then filter it, map it then return it?

I am trying to get data from the firestore and then filter it then map it like so:我试图从 firestore 获取数据,然后过滤它,然后像这样映射它:

return Inventory
.filter(x =>x["sub_category"] === item && x["category"] === category)
.map(({id, item, price, quantity, image})=>{

//sets the default value for the item with that specific id
const defVal = parseInt((selectedQty.id === id)?selectedQty.qty:0)

return (
    <React.Fragment key={uuid()}>
        <div className="card">
            <img src={image()} alt={item} />
            <p>{item}</p>
            
            <div className="innerBox">
                <div className="dropdown">
                    <label htmlFor="quantity">Qty:</label>
                    <select id="quantity" defaultValue={defVal===0?1:defVal} onChange={e => {
                        e.preventDefault()
                        setSelectedQty({id, qty: parseInt(e.target.value)})
                    }}>
                    {
                        Array(quantity).fill().map((_, i)=> {
                            if(i===0){
                                return <option key={uuid()} value={0}>-</option>
                            }else{
                                return <option key={uuid()} value={i} >{i}</option>
                            }
                        })
                    }
                    </select>
                </div>
                <b>$ {price}</b>
            </div>
            <button type="submit" onClick={()=> {
                addToCart(id, item, parseInt(finalQty), price, image(), parseInt(finalQty)*parseFloat(price))
                setSelectedQty({id:null, qty: 0})
            }}>Add to Cart</button>
        </div>
    </React.Fragment>
)})

Currently I am using Inventory Array but I want to switch to firestore but I have no clue how to do it.目前我正在使用 Inventory Array 但我想切换到 firestore 但我不知道如何去做。 I am aware of the step db.collection().get().then(etc...) but i don't know how to map it to return it inside the Then我知道步骤db.collection().get().then(etc...)但我不知道如何映射它以在Then 中返回它

When you fetch data from cloud firestore, it returns a document / collection snapshot.当您从 Cloud Firestore 获取数据时,它会返回一个文档/集合快照。

Document:文档:

db.collection("colName").doc("docID").get().then(docSnapShot=>{
    const docID = docSnapShot.id;
    const docData = docSnapShot.data();
})

Collection:收藏:

db.collection("colName").get().then(colSnapShot=>{
    const isEmpty = colSnapShot.empty;
    const docsData = colSnapShot.docs.forEach(docSnapShot=>{
        return docSnapShot.data();
    })
})

I believe your solution will look something like this:我相信您的解决方案将如下所示:

let arrOfDocs = await db.collection("colName").get().then(colSnapShot=>{
    return colSnapShot.docs.map(docSnapShot=>{
        return docSnapShot.data();
    })
})

Note that to listen to live updates, replace get().then(snapshot=>{}) with onSnapshot(snapshot=>{})请注意,要收听实时更新,请将get().then(snapshot=>{})替换为onSnapshot(snapshot=>{})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM