I have a react effector code here. How do I return an Error if the case if appeal.id is…
- User
I have a react effector code here. How do I return an Error if the case if appeal.id is equal to undefined? sample({ clock: sendStickerEv, target: sendSingleMessageFx, source: { appeal: appeals }, fn: ({ appeal, appeals }, stickerUrl) => { if (appeal?.id !== undefined) { const appealData = appeals.get(appeal?.id); const data: ISendedMessage = { mediaUrl: stickerUrl, mediaThumb: stickerUrl, text: '', templateId: null, replyId: appealData?.replyId, dialogId: appeal?.dialogId ?? 0, isSticker: true, }; return data; }
}, });
- Assistant
To return an error if appeal.id is equal to undefined, you can add a check at the beginning of the function and throw an Error if the condition is met. Here's how you can modify the code:
javascriptsample({ clock: sendStickerEv, target: sendSingleMessageFx, source: { appeal: $currentAppeal, appeals: $appeals }, fn: ({ appeal, appeals }, stickerUrl) => { if (appeal?.id === undefined) { throw new Error('Error: appeal.id is undefined'); } const appealData = appeals.get(appeal.id); const data: ISendedMessage = { mediaUrl: stickerUrl, mediaThumb: stickerUrl, text: '', templateId: null, replyId: appealData?.replyId, dialogId: appeal.dialogId ?? 0, isSticker: true, }; return data; }, });This way, if the condition
if (appeal?.id === undefined)is true, an error will be thrown and the function will not proceed further.