It does not render the authEmail function return value in the page . Can anyone help ?
import React, { Component }from ‘react’;
import { Accounts } from ‘meteor/accounts-base’;
import { render } from ‘react-dom’;
export default class EmailVerify extends Component{
constructor(props) {
super(props);
}
authEmail(token){
Accounts.verifyEmail(token, (error) =>{
if(error){
return(
error.reason
)
}else {
return(
<div> This is a good link</div>
)
}});
};
render() {
return(
<div>
{this.authEmail(this.props.params.token)}
</div>
)
}
}
what happens if you add:
componentDidMount() {
console.log( this.props.params.token );
}
It could be that the prop isn’t being passed to your component properly.
You can’t return a value from an async function. Instead set up the state of the component and then use setState to update the state and show the information.
Thanks Korus90 ! the token value is passed
Thanks coplekj ! How to make it not async ?
Try this:
constructor( props ) {
super( props );
this.state = {
token: ''
}
}
componentDidMount() {
this.setState({
token: this.props.params.token
});
}
authEmail() {
// If token is empty
if( this.state.token == '' ) {
console.log( 'No token passed' );
// Otherwise, if not empty run the Verify Email function
} else if( this.state.token != '' ) {
const token = this.state.token;
Accounts.verifyEmail( token, ( error ) => {
if( error ) {
console.log( error.reason );
} else {
return(
<div> This is a good link</div>
);
}
});
}
}
render() {
return(
<div>
{ this.authEmail() }
</div>
);
}
See if that works!
[EDIT] Made a couple of edits. Hopefully this will work!
korus90:
constructor( props ) {
super( props );
this.state = {
token: ‘’
}
}
componentDidMount() {
this.setState({
token: this.props.params.token
});
}
authEmail() {
// If token is empty
if( this.state.token == ‘’ ) {
console.log( ‘No token passed’ );
// Otherwise, if not empty run the Verify Email function
} else if( this.state.token != ‘’ ) {
const token = this.state.token;
Accounts.verifyEmail( token, ( error ) => {
if( error ) {
console.log( error.reason );
} else {
return(
<div> This is a good link</div>
);
}
});
}
}
render() {
return(
<div>
{ this.authEmail() }
</div>
);
}
Thanks for your detail reply ! But it has a problem : the method will be executed two times and show two logs : No token passed and then the error log. Actually the token is not set to the state … weird
Feel free to remove the first ‘if’, it was there to see if the prop was being passed properly!
If you can wait a few hours, I’ll try recreate this tonight and find a solution. I’m just on my way into work now so it’ll be a good 8 - 9 hours though!
Thanks Korus90 ! I will wait for your better code
korus90
January 10, 2017, 2:02pm
11
Sorry I started a new job yesterday and was well out of it when I got home, fell asleep!
I’ll do my best to have a look at this for you tonight, bare with me