gpt4 book ai didi

firebase - Vuexfire 将 Firestore 集合绑定(bind)到对象而不是列表

转载 作者:行者123 更新时间:2023-12-04 14:15:04 25 4
gpt4 key购买 nike

我的 Firestore 数据库中有以下集合:

scheme

我可以使用以下内容绑定(bind)到我的 vuex 商店:

state: {
assets: [],
},

actions: {
bindAssets: firestoreAction(({ bindFirestoreRef }) => {
return bindFirestoreRef('assets', db.collection('assets'))
}),
},

然而,这将它们绑定(bind)到 assets作为列表,即

[
{id: "id1" /* etc. */ },
{id: "id2" /* etc. */ },
{id: "id3" /* etc. */ },
]

在哪里,我希望它被绑定(bind)为:

{
"id1": { /* etc. */ },
"id2": { /* etc. */ },
"id3": { /* etc. */ },
}

我怎样才能做到这一点?

最佳答案

如果要改造assets数组(通过绑定(bind) assets 集合获得)在一个对象中,如下所示

{
"id1": { /* etc. */ },
"id2": { /* etc. */ },
"id3": { /* etc. */ },
}
以下 Vuex getter 应该可以解决问题:
state: {
assets: [],
},

actions: {
bindAssets: firestoreAction(({ bindFirestoreRef }) => {
return bindFirestoreRef('assets', db.collection('assets'))
}),
},

getters: {
assetsObj: state => {

const arrayToObject = (array) =>
array.reduce((obj, item) => {
obj[item.id] = item
return obj
}, {})

return arrayToObject(state.assets)

}
}

更新您的评论(在聊天中):
如果你只想绑定(bind)一个文档,你应该这样做,用 bindAssetDocassetDoc .
店铺
state: {
assets: [],
assetDoc: null
},

mutations: vuexfireMutations,

actions: {
bindAssetDoc: firestoreAction(({ bindFirestoreRef }, payload) => {
return bindFirestoreRef('assetDoc', db.collection('assets').doc(payload.id))
}),
bindAssets: firestoreAction(({ bindFirestoreRef }) => {
return bindFirestoreRef('assets', db.collection('assets'))
})
}
COMPONENT开通通过/asset/:assetId
<script>
//...
export default {
created() {
console.log('OK1');
this.$store
.dispatch('bindAssetDoc', { id: this.$route.params.assetId });
}
};
</script>

关于firebase - Vuexfire 将 Firestore 集合绑定(bind)到对象而不是列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61097916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com