2017-05-28 37 views
0

我构建了一个应用程序,其中包含reactxs & redux并面临以下问题。无法访问带有道具的响应对象

所以基本上我有这样的功能:

onAdmissionFormSubmit(e){ 
e.preventDefault(); 
const { user } = this.props 
console.log(user); 
// If I do this const id = user.member.externalMemberId .... 
// doesnt throw an error but if I log my const id it just says its undefined 
} 

它实际上是记录我的对象,但如果要访问像user.member.externalMemberId对象内的数据,它实际上犯规返回一个错误,但它只是说未定义。 即时通讯做错了什么?

的用户

控制台日志:http://prntscr.com/fcxmou

+0

好像'member'是不确定的,然后。你确定它在吗? –

+0

如果你记录这个'user.member.externalMemberId',会发生什么? –

+0

'未捕获的类型错误:无法读取未定义的属性'成员' 那我得到......奇怪的权利? – Emmanuel

回答

0

根据您的意见似乎userundefined,导致我相信,要么你没有user对象this.props或者你没有props对象在this
如果是第二选择,那么可能会发生如果您没有bind处理程序方法进入class。在这种情况下,this引用触发处理程序的元素,而不是像您期望的那样触发类。
你应该做的classconstructor的结合:

constructor(props) { 
    super(props); 
    this.onAdmissionFormSubmit = this.onAdmissionFormSubmit.bind(this); 
} 
+0

是的,我忘了绑定它! – Emmanuel