如何从Firestore文档上的subArray中删除对象?

时间:2019-07-08 04:24:40

标签: javascript arrays node.js google-cloud-firestore

我已经能够根据产品的类别编写代码以将对象添加到相应的数组中,但是使用相同的逻辑我无法删除它。有人知道怎么做吗?

Firestore组织(要删除的字段带有红色箭头,数组组件位于文档中):

https://drive.google.com/file/d/1bI_xT5nmrUtFcWQefjZCXLidA3aonaGm/view?usp=sharing

async addComponentToBuild(userId, buildId, component) {**//THIS METHOD IS WORKING**
        const fieldName = `components.${component.CATEGORY}`
        const updateObj = {};const compObj ={}
        compObj[component.PRODUCTNAME] = component
        updateObj[fieldName] = FieldValue.arrayUnion(compObj)
        return this.usersDb.doc(userId).collection('builds').doc(buildId)
            .update(updateObj)
            .then(doc => { `Component ${component.PRODUCTNAME} added`})
            .catch(handleError)
    }

    async deleteComponentFromBuild(userId, buildId, componentId) {
        var component = await this.getSingleComponent(componentId)
        var fieldName = `components.${component.CATEGORY}`//.${componentId}
        const updateObj = {}
        updateObj[fieldName] = FieldValue.arrayRemove(componentId)
        return this.usersDb.doc(userId).collection('builds').doc(buildId).update(updateObj)
            .then(doc => 
                { `Component ${component.PRODUCTNAME} deleted`}
            )
            .catch(handleError)
    }

0 个答案:

没有答案