admin管理员组文章数量:1390503
I am finding hard to use Flow. I understand that Array.find
can return or be undefined.
So by reading this: github: Array.find on Array<{...}> raises
I did that and added void
to the type to. However now I get the error:
Cannot get
stageMsg.msg
because propertymsg
is missing in null or undefined (see line 92).
Check code to see where the error is specifically.
I understand why it is giving me the error, there is no msg
in undefined. what I don't know is how to solve this issue.
Here is the code:
type StageMsg = {
stage: number,
msg?: string
};
let stageMsg: void | StageMsg = this.stages.find(
(obj: StageMsg) => obj.stage === this.state.stage
);
msg = (
<Msg text={this.state.msg !== "" ? this.state.msg : stageMsg.msg} /> //<---- [Error here].
);
I am finding hard to use Flow. I understand that Array.find
can return or be undefined.
So by reading this: github: Array.find on Array<{...}> raises
I did that and added void
to the type to. However now I get the error:
Cannot get
stageMsg.msg
because propertymsg
is missing in null or undefined (see line 92).
Check code to see where the error is specifically.
I understand why it is giving me the error, there is no msg
in undefined. what I don't know is how to solve this issue.
Here is the code:
type StageMsg = {
stage: number,
msg?: string
};
let stageMsg: void | StageMsg = this.stages.find(
(obj: StageMsg) => obj.stage === this.state.stage
);
msg = (
<Msg text={this.state.msg !== "" ? this.state.msg : stageMsg.msg} /> //<---- [Error here].
);
Share
Improve this question
edited Sep 3, 2020 at 11:11
mkrieger1
23.5k7 gold badges64 silver badges82 bronze badges
asked Nov 14, 2018 at 14:11
Rip3rsRip3rs
1,57013 silver badges22 bronze badges
1 Answer
Reset to default 8You just need to check that stageMsg
exists before accessing a property on it.
So you could wrap your assigment to msg
in if (stageMsg) { ... }
, or use an inline expression like stageMsg && stageMsg.msg
.
Also note that you could use maybe syntax instead of a union with void
e.g. let stageMsg: ?StageMsg
本文标签: javascriptFlowtype property 39msg39 is missing in null or undefinedStack Overflow
版权声明:本文标题:javascript - Flowtype property 'msg' is missing in null or undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744608930a2615531.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论