Reactjs 文档内数组中的firestore更新值

Reactjs 文档内数组中的firestore更新值,reactjs,firebase,google-cloud-firestore,Reactjs,Firebase,Google Cloud Firestore,我看了好几个小时的firestore文档,仍然没有找到这个案例的解决方案。 我需要在我的电子商务应用程序中添加一个AMOUT功能 数据结构: 主收藏是“购物车”,文档是用户电子邮件 以下是添加或设置产品的当前代码: import firebase from 'firebase'; export async function addToCart(userMail, itemId, name, url, price, category, type, description) { cons

我看了好几个小时的firestore文档,仍然没有找到这个案例的解决方案。 我需要在我的电子商务应用程序中添加一个AMOUT功能

数据结构: 主收藏是“购物车”,文档是用户电子邮件

以下是添加或设置产品的当前代码:

import firebase from 'firebase';


export async function addToCart(userMail, itemId, name, url, price, category, type, description) {
    const ammout = 1
    const cartItem = { itemId, name, url, price, category, type, description, ammout }
    if (userMail === undefined) throw console.error('please Login');

    const userDoc = await firebase.firestore().collection("cart").doc(userMail)

    await userDoc.get().then((doc) => {
        if (doc.exists) {
            (async () => {
                    

                await userDoc.update({
                    item: firebase.firestore.FieldValue.arrayUnion(cartItem),
                })
            })()
        }
        else {
            (async () => {
                await userDoc.set({
                    item: firebase.firestore.FieldValue.arrayUnion(cartItem)
                })
            })()
        }

    })
}

问题可能是您同时使用了
await
Promise.then()
在同一个函数中,这可能会产生一些同步性问题,而且您不需要
await
来填充
userDoc
变量,正如您在上看到的那样

因此,基本上我会从代码中删除所有的
async/await
,因为它们不是真正需要的,并且
承诺。然后
.get()
上的()

export function addToCart(userMail, itemId, name, url, price, category, type, description) {
    const ammout = 1
    const cartItem = { itemId, name, url, price, category, type, description, ammout }
    if (userMail === undefined) throw console.error('please Login');

    const userDoc = firebase.firestore().collection("cart").doc(userMail)

    userDoc.get().then((doc) => {
        if (doc.exists) {
            userDoc.update({
                item: firebase.firestore.FieldValue.arrayUnion(cartItem),
            })
        }
        else {
            userDoc.set({
                item: firebase.firestore.FieldValue.arrayUnion(cartItem)
            })
        }

    })
}

@YanivCode会这样吗?那是另一个问题我会查的,泰。@YanivCode你说那是另一个问题是什么意思?