1

When I press the confirm button, I update the user's info in the database. Then I need to do a SOAP request, and call a send mail function.

router.put('/users/:id', function (req, res) {
    var passwordHash = null;
    if(req.body.password !== undefined && req.body.password.length > 3){
        passwordHash = bcrypt.hashSync(req.body.password, bcryptSalt);
    }
    db.one('update "TUsers" ' +
        'set "username"=$1, "firstname"=$2, "lastname"=$3, "updatedAt"=now(), "pin"=$4, "rayon_id"=$5, "status"=$7' + ((passwordHash)?', "password"=$8 ':'') +
        'where id=$6 returning id, username, firstname, lastname, pin, rayon_id, status',
        [req.body.username, req.body.firstname, req.body.lastname, req.body.pin, req.body.rayon_id, req.params.id, req.body.status, passwordHash])
        .then(function (data) {
            console.log(req.body.isConfirm);
            if(req.body.isConfirm != false){
                putUser(res, req.body.username, req.body.mail, passwordHash, req.body.firstname, req.body.lastname);
                sendMail(req.body.username, req.body.password, req.body.system, req.body.mail);

            }
            res.json(data);
        })
        .catch(function (error) {
            console.log(error);
            res.status(400).json({error: error});
        });
});

Below the sendMail() and putUser(). The putUser() creates a SOAP request to the JasperServer API and puts a new record when updating the user's info on my local database.

//SendMail Function
 function sendMail(username, password, systemID, emailTo) {

    // setup e-mail data with unicode symbols
    var mailOptions = {
        from: '"Администрация" <admindrts@infocom.kg>', // sender address
        to: '"'+emailTo+'"', // list of receivers
        subject: 'Регистрация подтверждена.', // Subject line
        text: 'Ваша учетная запись подтверждена в системе "'+systemID+'" под логином "'+username+'", пароль для доступа к системе: "'+password+'"' // plaintext body
        //html: '<b>Hello world ?</b>' // html body
    };

    // send mail with defined transport object
    smtpTransport.sendMail(mailOptions, function(error, info){
        if(error){
            return console.log(error);
        }
        console.log('Message sent: ' + info.response);
    });
}



function putUser(res, username, emailTo, password, firstname, lastname) {
    var methodName_j = "putUser";
    var fullName = firstname + " " + lastname;


    var param_j ={'user':{
        'username': username,
        'fullName': fullName,
        'password': password,
        'emailAddress': emailTo,
        'externallyDefined': false,
        'enabled': true
    }};

    return soap.createClient(soap_url_j, { wsdl_headers: {Authorization: auth} }, function (err, client) {

        if (err) {
            console.log(err);
            return;
        }
        client.addHttpHeader('Authorization',auth);
        client[methodName_j](param_j, function (err, result) {
            console.log(result);
            if (err) {
                return res.json(err);
            }
            return res.json(result);

        });
    });
}
Stacked
  • 6,892
  • 7
  • 57
  • 73
  • Possible duplicate [here](http://stackoverflow.com/questions/20404822/error-cant-set-headers-after-they-are-sent) – Stacked Oct 31 '16 at 07:21
  • Possible duplicate of [How to fix "Headers already sent" error in PHP](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Paul Sweatte Sep 11 '17 at 18:42

0 Answers0