Error 6412 – 无法分配给只读属性

huangapple go评论111阅读模式
英文:

Error 6412 - Cannot assign to read only property

问题

I am trying to update the state model, but I am running into an issue (because I do not have enough experience with it yet). Getting "core.mjs:6412 ERROR TypeError: Cannot assign to read-only property 'decisionDate' of object '[object Object]'".

The issue is at the line I added "-->" to. What have I done wrong/what am I missing?

UPDATE and SOLUTION - 07-03-2023
So, I had to:

  • I had to clone the original item (use the react ...)
  • Then I had to replace the existing element of the collection, with the newly cloned element (along with its change).
  • Then patchState
const DocumentList = ctx.getState().request.documents;
const DocumentIndex = DocumentList.findIndex(item => item.guid === action.documentGuid);
var ItemToUpdate = {...DocumentList[DocumentIndex]};

ItemToUpdate.decisionDate = action.newDate;

const reconstruct = addOrReplace('guid', DocumentList, ItemToUpdate);

Hope this helps someone.

英文:

I am trying to update the state model, but I am running into an issue (because I do not have enough experience with it yet). Getting "core.mjs:6412 ERROR TypeError: Cannot assign to read only property 'decisionDate' of object '[object Object]'".

The issue is at the line I added "-->" to. What have I done wrong/what am I missing?

@Action(ClerkAction.Review.UpdateSelectedDate)  
    onUpdateSelectedDate(ctx: StateContext<ClerkStateModel>, action: ClerkAction.Review.UpdateSelectedDate) {
        const DocumentList = ctx.getState().request.documents;
        const DocumentIndex = DocumentList.findIndex(item => item.guid === action.documentGuid);  
-->     DocumentList[DocumentIndex].decisionDate = action.newDate;  

		ctx.patchState({ 
			request: {
				...ctx.getState().request,
				documents: DocumentList
			}        
        });
	
		ctx.dispatch(new NotificationAction.Loading(false));
    }  

UPDATE and SOLUTION - 07-03-2023
So, I had to:

  • I had to clone the original item (use the react ...)

  • Then I had to replace the existing element of the collection, with the newly cloned element (along with its change).

  • Then patchState

      const DocumentList = ctx.getState().request.documents;
      const DocumentIndex = DocumentList.findIndex(item => item.guid === action.documentGuid);
      var ItemToUpdate = {...DocumentList[DocumentIndex]};
    
      ItemToUpdate.decisionDate = action.newDate;
    
      const reconstruct = addOrReplace('guid', DocumentList, ItemToUpdate);
    

Hope this helps someone.

答案1

得分: 0

另一个选项是使用StateOperators

在你的情况下,应该是这样的:

ctx.setState(
patch({
documents: updateItem(
item => item.guid === action.documentGuid,
patch({ decisionDate: action.newDate }
)
})
);

英文:

Another option is to use StateOperators

In your case it should be something like:

> ctx.setState(
> patch<ClerkStateModel>({
> documents: updateItem(
> item => item.guid === action.documentGuid,
> patch({ decisionDate: action.newDate }
> )
> })
> );

huangapple
  • 本文由 发表于 2023年3月7日 01:02:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定