Hey All,
I’m using React and React Router v4.2 with my Meteor app. I’m trying to get javascript to push my history after an account gets registered.
Though I keep getting this error:
Exception in delivering result of invoking ‘createUser’: TypeError: Cannot read property ‘history’ of undefined
Here is my main app with the Router setup:
App.jsx
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
export default class Layout extends React.Component {
render() {
return (
<Router>
<div>
<Header />
<Switch>
<Route exact path="/" render={(props) => <Home {...props} /> } />
<Route exact path='/register' render={(props) => <RegisterForm {...props} /> } />
<Route path = '/dashboard' render={(props) => <Home {...props} /> } />
</Switch>
</div>
</Router>
And then the Register form:
RegisterForm.jsx
import { Meteor } from 'meteor/meteor';
import React from 'react';
import ReactDOM from 'react-dom';
export default class RegisterForm extends React.Component {
constructor(props) {
super(props)
}
createUser(e){
e.preventDefault();
const email = $('#emailReg').val();
const password = $('#pwdReg').val().trim();
Accounts.createUser({ email: email, password: password },
function(error){
if (error) { console.log ("there was an error: " + error); }
else {
console.log("Account Created with email " + email);
this.props.history.push("/dashboard");
}
});
}
render(){
return(
<form role="form" id="registerForm" onSubmit={this.createUser}>...etc...</form>
)
}
}
The error occurs when the form is submitted. All i want to do for now is to figure out why I can’t get history to be defined and change the component as expected. I’ll worry about protecting components later.
What am I doing wrong?