Reputation: 57
I am new in node js and I stack with this error longtime. Can someone help me to find a solution! here is the code taht I have written !
router.get('/list',function (req, res, next)
{
res.render('listaccounts.ejs',{mybalance:mybalance});
});
//accounts creation
router.post('/', function(req, res, next) {
var passphrase = req.body.passphrase;
var mybalance=web3.fromWei(web3.eth.getBalance(web3.eth.accounts[0]));
var accountAddress=web3_.personal.newAccount("passphrase",function(error,result){
if(!error){
console.log(result);
}
else {
res.render('error.ejs');
}
});
res.redirect('/accounts/list');
});
Upvotes: 0
Views: 42
Reputation: 2845
Just as Jakub said. Also you maty have an async execution problem (you don't know what will execute first, the redirect or the render). I think you could simply:
var accountAddress=web3_.personal.newAccount("passphrase",function(error,result){
if(!error){
console.log(result);
res.redirect('/accounts/list');
}
else {
res.render('error.ejs');
}
});
Upvotes: 1
Reputation: 1038
The problem lays in
if(!error){
console.log(result);
}
else {
res.render('error.ejs');
}
});
res.redirect('/accounts/list');
When it catches else statement, it does render the page, then you call page redirect, but headers were sent during res.render. In short words, you can't render and redirect at the same time
Upvotes: 1