UnhandledPromiseRejectionWarning: Ошибка [ERR_HTTP_HEADERS_SENT] - PullRequest
0 голосов
/ 17 октября 2019

Я зарегистрировал участника в сети, но при попытке войти с учетными данными участника я получаю эту ошибку:

(узел: 8931) UnhandledPromiseRejectionWarning: Ошибка [ERR_HTTP_HEADERS_SENT]: невозможноустановите заголовки после их отправки клиенту по адресу ServerResponse.setHeader (_http_outgoing.js: 481: 11) по адресу ServerResponse.header (/ home / pavansoratur / Desktop / lpbc-changes / web-app / node_modules / express / lib / response). js: 767: 10) в ServerResponse.send (/home/pavansoratur/Desktop/lpbc-changes/web-app/node_modules/express/lib/response.js:170:12) в ServerResponse.json (/ home / pavansoratur /Рабочий стол / lpbc-changes / web-app / node_modules / express / lib / response.js: 267: 15) по адресу network.allMembersInfo.then (/home/pavansoratur/Desktop/lpbc-changes/web-app/app.js:418: 65) (узел: 8931) UnhandledPromiseRejectionWarning: необработанное отклонение обещания. Эта ошибка возникла либо в результате выброса внутри асинхронной функции без блока catch, либо в результате отклонения обещания, которое не было обработано с помощью .catch (). (идентификатор отклонения: 2) (узел: 8931) [DEP0018] Предупреждение об устаревании: отклонения необработанного обещания устарели. В будущем отклонения обещаний, которые не обрабатываются, завершат процесс Node.js с ненулевым кодом выхода.

Это мой код app.js:


//get libraries
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const path = require('path')

//create express web-app
const app = express();
const router = express.Router();

//get the libraries to call
var network = require('./network/network.js');
var validate = require('./network/validate.js');
var analysis = require('./network/analysis.js');

//bootstrap application settings
app.use(express.static('./public'));
app.use('/scripts', express.static(path.join(__dirname, '/public/scripts')));
app.use(bodyParser.json());

//get home page
app.get('/home', function(req, res) {
  res.sendFile(path.join(__dirname + '/public/index.html'));
});

//get member page
app.get('/member', function(req, res) {
  res.sendFile(path.join(__dirname + '/public/member.html'));
});

//get member registration page
app.get('/registerMember', function(req, res) {
  res.sendFile(path.join(__dirname + '/public/registerMember.html'));
});

//get partner page
app.get('/partner', function(req, res) {
  res.sendFile(path.join(__dirname + '/public/partner.html'));
});

//get partner registration page
app.get('/registerPartner', function(req, res) {
  res.sendFile(path.join(__dirname + '/public/registerPartner.html'));
});

//get about page
app.get('/about', function(req, res) {
  res.sendFile(path.join(__dirname + '/public/about.html'));
});


//post call to register member on the network
app.post('/api/registerMember', function(req, res) {

  //declare variables to retrieve from request
  var accountNumber = req.body.accountnumber;
  var cardId = req.body.cardid;
  var firstName = req.body.firstname;
  var lastName = req.body.lastname;
  var email = req.body.email;
  var phoneNumber = req.body.phonenumber;

  //print variables
  console.log('Using param - firstname: ' + firstName + ' lastname: ' + lastName + ' email: ' + email + ' phonenumber: ' + phoneNumber + ' accountNumber: ' + accountNumber + ' cardId: ' + cardId);

  //validate member registration fields
  validate.validateMemberRegistration(cardId, accountNumber, firstName, lastName, email, phoneNumber)
    .then((response) => {
      //return error if error in response
      if (response.error != null) {
        res.json({
          error: response.error
        });
        return;
      } else {
        //else register member on the network
        network.registerMember(cardId, accountNumber, firstName, lastName, email, phoneNumber)
          .then((response) => {
            //return error if error in response
            if (response.error != null) {
              res.json({
                error: response.error
              });
            } else {
              //else return success
              res.json({
                success: response
              });
            }
          });
        }
    });
});

//post call to register partner on the network
app.post('/api/registerPartner', function(req, res) {

  //declare variables to retrieve from request
  var name = req.body.name;
  var partnerId = req.body.partnerid;
  var cardId = req.body.cardid;

  //print variables
  console.log('Using param - name: ' + name + ' partnerId: ' + partnerId + ' cardId: ' + cardId);

  //validate partner registration fields
  validate.validatePartnerRegistration(cardId, partnerId, name)
    .then((response) => {
      //return error if error in response
      if (response.error != null) {
        res.json({
          error: response.error
        });
        return;
      } else {
        //else register partner on the network
        network.registerPartner(cardId, partnerId, name)
          .then((response) => {
            //return error if error in response
            if (response.error != null) {
              res.json({
                error: response.error
              });
            } else {
              //else return success
              res.json({
                success: response
              });
            }
          });
      }
    });
});

//post call to perform EarnPoints transaction on the network
app.post('/api/earnPoints', function(req, res) {

  //declare variables to retrieve from request
  var accountNumber = req.body.accountnumber;
  var cardId = req.body.cardid;
  var partnerId = req.body.partnerid;
  var points = parseFloat(req.body.points);

  //print variables
  console.log('Using param - points: ' + points + ' partnerId: ' + partnerId + ' accountNumber: ' + accountNumber + ' cardId: ' + cardId);

  //validate points field
  validate.validatePoints(points)
    .then((checkPoints) => {  
      //return error if error in response
      if (checkPoints.error != null) {
        res.json({
          error: checkPoints.error
        });
        return;
      } else {
        points = checkPoints;
        //else perforn EarnPoints transaction on the network
        network.earnPointsTransaction(cardId, accountNumber, partnerId, points)
          .then((response) => {
            //return error if error in response
            if (response.error != null) {
              res.json({
                error: response.error
              });
            } else {
              //else return success
              res.json({
                success: response
              });
            }
          });
        }
    });
});

//post call to perform UsePoints transaction on the network
app.post('/api/usePoints', function(req, res) {

  //declare variables to retrieve from request
  var accountNumber = req.body.accountnumber;
  var cardId = req.body.cardid;
  var partnerId = req.body.partnerid;
  var points = parseFloat(req.body.points);

  //print variables
  console.log('Using param - points: ' + points + ' partnerId: ' + partnerId + ' accountNumber: ' + accountNumber + ' cardId: ' + cardId);

  //validate points field
  validate.validatePoints(points)
    //return error if error in response
    .then((checkPoints) => {
      if (checkPoints.error != null) {
        res.json({
          error: checkPoints.error
        });
        return;
      } else {
        points = checkPoints;
        //else perforn UsePoints transaction on the network
        network.usePointsTransaction(cardId, accountNumber, partnerId, points)
          .then((response) => {
            //return error if error in response
            if (response.error != null) {
              res.json({
                error: response.error
              });
            } else {
              //else return success
              res.json({
                success: response
              });
            }
          });
        }
    });
});

//post call to perform SendPoints transaction on the network
app.post('/api/sendPoints', function(req, res) {

  //declare variables to retrieve from request
  var accountNumber = req.body.accountnumber;
  var cardId = req.body.cardid;
  var phoneNumber = req.body.phoneNumber;
  var points = parseFloat(req.body.points);

  //print variables
  console.log('Using param - points: ' + points + ' phoneNumber: ' + phoneNumber + ' accountNumber: ' + accountNumber + ' cardId: ' + cardId);

  //validate points field
  validate.validatePoints(points)
    //return error if error in response
    .then((checkPoints) => {
      if (checkPoints.error != null) {
        res.json({
          error: checkPoints.error
        });
        return;
      } else {
        points = checkPoints;
        //else perforn SendPoints transaction on the network
        network.sendPointsTransaction(cardId, accountNumber, phoneNumber, points)
          .then((response) => {
            //return error if error in response
            if (response.error != null) {
              res.json({
                error: response.error
              });
            } else {
              //else return success
              res.json({
                success: response
              });
            }
          });
        }
    });
});

//post call to perform GetPoints transaction on the network
app.post('/api/getPoints', function(req, res) {

    //declare variables to retrieve from request
    var accountNumber = req.body.accountnumber;
    var cardId = req.body.cardid;
    var phoneNumber = req.body.phoneNumber;
    var points = parseFloat(req.body.points);

    //print variables
    console.log('Using param - points: ' + points + ' phoneNumber: ' + phoneNumber + ' accountNumber: ' + accountNumber + ' cardId: ' + cardId);

    //validate points field
    validate.validatePoints(points)
      //return error if error in response
      .then((checkPoints) => {
        if (checkPoints.error != null) {
          res.json({
            error: checkPoints.error
          });
          return;
        } else {
          points = checkPoints;
          //else perforn SendPoints transaction on the network
          network.getPointsTransaction(cardId, accountNumber, phoneNumber, points)
            .then((response) => {
              //return error if error in response
              if (response.error != null) {
                res.json({
                  error: response.error
                });
              } else {
                //else return success
                res.json({
                  success: response
                });
              }
            });
          }
      });
  });

//post call to retrieve member data, transactions data and partners to perform transactions with from the network
app.post('/api/memberData', function (req, res) {

    //declare variables to retrieve from request
    var accountNumber = req.body.accountnumber;
    var cardId = req.body.cardid;

    //print variables
    console.log('memberData using param - ' + ' accountNumber: ' + accountNumber + ' cardId: ' + cardId);

    //declare return object
    var returnData = {};

    //get member data from network
    network.memberData(cardId, accountNumber)
        .then((member) => {
            //return error if error in response
            if (member.error != null) {
                res.json({
                    error: member.error
                });
            } else {
                //else add member data to return object
                returnData.accountNumber = member.accountNumber;
                returnData.firstName = member.firstName;
                returnData.lastName = member.lastName;
                returnData.phoneNumber = member.phoneNumber;
                returnData.email = member.email;
                returnData.points = member.points;
            }

        }).then(() => {
            //get UsePoints transactions from the network
            network.usePointsTransactionsInfo(cardId, 'member', accountNumber)
                .then((usePointsResults) => {
                    //return error if error in response
                    if (usePointsResults.error != null) {
                        res.json({
                            error: usePointsResults.error
                        });
                    } else {
                        //else add transaction data to return object
                        returnData.usePointsResults = usePointsResults;
                    }

                }).then(() => {
                    //get SendPoints transactions from the network
                    network.sendPointsTransactionsInfo(cardId, 'member', accountNumber)
                        .then((sendPointsResults) => {
                            //return error if error in response
                            if (sendPointsResults.error != null) {
                                res.json({
                                    error: sendPointsResults.error
                                });
                            } else {
                                //else add transaction data to return object
                                returnData.sendPointsResults = sendPointsResults;
                            }
                        }).then(() => {
                            //get EarnPoints transactions from the network
                            network.earnPointsTransactionsInfo(cardId, 'member', accountNumber)
                                .then((earnPointsResults) => {
                                    //return error if error in response
                                    if (earnPointsResults.error != null) {
                                        res.json({
                                            error: earnPointsResults.error
                                        });
                                    } else {
                                        //else add transaction data to return object
                                        returnData.earnPointsResult = earnPointsResults;
                                    }
                                }).then(() => {
                                    //get GetPoints transactions from the network
                                    network.getPointsTransactionsInfo(cardId, 'member', accountNumber)
                                        .then((getPointsResults) => {
                                            //return error if error in response
                                            if (getPointsResults.error != null) {
                                                res.json({
                                                    error: getPointsResults.error
                                                });
                                            } else {
                                                //else add transaction data to return object
                                                returnData.getPointsResult = getPointsResults;
                                            }
                                        }).then(() => {
                                            //get partners to transact with from the network
                                            network.allPartnersInfo(cardId)
                                                .then((partnersInfo) => {
                                                    //return error if error in response
                                                    if (partnersInfo.error != null) {
                                                        res.json({
                                                            error: partnersInfo.error
                                                        });
                                                    } else {
                                                        //else add partners data to return object
                                                        returnData.partnersData = partnersInfo;
                                                    }
                                                    //return returnData
                                                    res.json(returnData);
                                                }).then(() => {
                                                    //get members to transact with from the network
                                                    network.allMembersInfo(cardId)
                                                        .then((membersInfo) => {
                                                            //return error if error in response
                                                            if (membersInfo.error != null) {
                                                                res.json({
                                                                    error: membersInfo.error
                                                                });
                                                            } else {
                                                                //else add members data to return object
                                                                returnData.membersData = membersInfo;
                                                            }
                                                            //return returnData
                                                            res.json(returnData);
                                                        });
                                                });
                                        });
                                });
                        });
                });
        }); 
});

//post call to retrieve partner data and transactions data from the network
app.post('/api/partnerData', function (req, res) {
    //declare variables to retrieve from request
    var partnerId = req.body.partnerid;
    var cardId = req.body.cardi
    //print variables
    console.log('partnerData using param - ' + ' partnerId: ' + partnerId + ' cardId: ' + cardId);
    //declare return object
    var returnData = {}
    //get partner data from network
    network.partnerData(cardId, partnerId)
        .then((partner) => {
            //return error if error in response
            if (partner.error != null) {
                res.json({
                    error: partner.error
                });
            } else {
                //else add partner data to return object
                returnData.id = partner.id;
                returnData.name = partner.name;
            }
        }).then(() => {
            //get UsePoints transactions from the network
            network.usePointsTransactionsInfo(cardId, 'partner', partnerId)
                .then((usePointsResults) => {
                    //return error if error in response
                    if (usePointsResults.error != null) {
                        res.json({
                            error: usePointsResults.error
                        });
                    } else {
                        //else add transaction data to return object
                        returnData.usePointsResults = usePointsResults;
                        //add total points collected by partner to return object
                        returnData.pointsCollected = analysis.totalPointsCollec(usePointsResults);
                    }
                }).then(() => {
                    //get EarnPoints transactions from the network
                    network.earnPointsTransactionsInfo(cardId, 'partner', partnerId)
                        .then((earnPointsResults) => {
                            //return error if error in response
                            if (earnPointsResults.error != null) {
                                res.json({
                                    error: earnPointsResults.error
                                });
                            } else {
                                //else add transaction data to return object
                                returnData.earnPointsResults = earnPointsResults;
                                //add total points given by partner to return object
                                returnData.pointsGiven = analysis.totalPointsGi(earnPointsResul);
                            }
                            //return returnData
                            res.json(returnData);
                        });
                });
        });
});


 //declare port
 var port = process.env.PORT || 5000;
 // if (process.env.VCAP_APPLICATION) {
 //   port = process.env.PORT;
 // }
 //run app on port
 app.listen(port, function () {
     console.log(`app running on port: ${port}`);
 });

Как я могу решить эту проблему?

...