我正在用React-Meteor
构建一个应用程序,但其行为的随机性存在问题。当使用部分应用程序时,我收到一个错误,上面写着:
无法读取未定义的属性X
但X(及其渲染)来自不同的页面,通常来自我以前所在的页面,有时来自许多其他页面。但是,如果我刷新并尝试,它将不会产生错误。
只有一个显示时的错误如下:
Exception from Tracker recompute function:
debug.js:41 TypeError: Cannot read property 'jobId' of undefined
at React.createClass.getMeteorData (user.jsx:209)
at meteor-data-mixin.jsx:89
at Tracker.Computation._compute (tracker.js:323)
at new Tracker.Computation (tracker.js:211)
at Object.Tracker.autorun (tracker.js:562)
at meteor-data-mixin.jsx:76
at Object.Tracker.nonreactive (tracker.js:589)
at MeteorDataManager.calculateData (meteor-data-mixin.jsx:75)
at ReactMeteorData.componentWillUpdate (meteor-data-mixin.jsx:22)
at ReactCompositeComponentMixin._performComponentUpdate (ReactCompositeComponent.js:535)
在某些情况下,会从其他几个页面中显示多个具有不同X的页面。
这是我所在页面的代码:
Template.addNotification.onRendered(function() {
ReactDOM.render(<AddNotification />,
document.getElementById("addNotification"));
});
AddNotification = React.createClass({
addNotification: function(evt) {
evt.preventDefault();
notificationInfo = {
message: ReactDOM.findDOMNode(this.refs.msg).value,
notifType: ReactDOM.findDOMNode(this.refs.notifType).value,
user: ReactDOM.findDOMNode(this.refs.user).value
};
Meteor.call("addNotification", notificationInfo);
},
addBunch: function(evt) {
evt.preventDefault();
Meteor.call("addBunch", Meteor.userId());
},
render() {
return (
<div>
<div>Message:<input type="text" ref="msg"/></div>
<div>Type:<input type="text" ref="notifType"/></div>
<div>User:<input type="text" ref="user"/></div>
<button onClick={this.addNotification}>Add</button>
<button onClick={this.addBunch}>
Or, add a bunch to current</button>
</div>
);
}
});
你可以看到,我甚至没有在这个页面上调用任何需要jobId
的东西。
这听起来像是一个需要警卫的情况。看看错误的来源,并用类似的东西来保护它:
obj && obj.jobId
其中obj
是潜在的未定义对象。