From c8fac08e0b0342c571ff5d1cc3e02220eb1a9894 Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Thu, 8 Jun 2023 20:12:14 -0700 Subject: [PATCH] fix: Cannot read properties of undefined (reading 'some') (#7278) Fixes https://linear.app/chatwoot/issue/CW-2041/typeerror-cannot-read-properties-of-undefined-reading-some Fixes https://linear.app/chatwoot/issue/CW-2042/typeerror-cannot-read-properties-of-undefined-reading-filter --- .../dashboard/store/modules/conversations/index.js | 9 ++++----- .../store/modules/specs/conversations/mutations.spec.js | 8 ++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/javascript/dashboard/store/modules/conversations/index.js b/app/javascript/dashboard/store/modules/conversations/index.js index 2062b07da..026d51d55 100644 --- a/app/javascript/dashboard/store/modules/conversations/index.js +++ b/app/javascript/dashboard/store/modules/conversations/index.js @@ -58,11 +58,10 @@ export const mutations = { } }, [types.SET_ALL_ATTACHMENTS](_state, { id, data }) { - if (data.length) { - const [chat] = _state.allConversations.filter(c => c.id === id); - Vue.set(chat, 'attachments', []); - chat.attachments.push(...data); - } + const [chat] = _state.allConversations.filter(c => c.id === id); + if (!chat) return; + Vue.set(chat, 'attachments', []); + chat.attachments.push(...data); }, [types.SET_MISSING_MESSAGES](_state, { id, data }) { const [chat] = _state.allConversations.filter(c => c.id === id); diff --git a/app/javascript/dashboard/store/modules/specs/conversations/mutations.spec.js b/app/javascript/dashboard/store/modules/specs/conversations/mutations.spec.js index c7280c6f9..279b36872 100644 --- a/app/javascript/dashboard/store/modules/specs/conversations/mutations.spec.js +++ b/app/javascript/dashboard/store/modules/specs/conversations/mutations.spec.js @@ -288,6 +288,14 @@ describe('#mutations', () => { mutations[types.SET_ALL_ATTACHMENTS](state, { id: 1, data }); expect(state.allConversations[0].attachments).toEqual(data); }); + it('set attachments key even if the attachments are empty', () => { + const state = { + allConversations: [{ id: 1 }], + }; + const data = []; + mutations[types.SET_ALL_ATTACHMENTS](state, { id: 1, data }); + expect(state.allConversations[0].attachments).toEqual([]); + }); }); describe('#ADD_CONVERSATION_ATTACHMENTS', () => {