UnhandledPromiseRejectionWarning:错误[ERR_HTTP_HEADERS_SENT]

时间:2019-10-17 16:12:18

标签: javascript json promise deprecation-warning unhandled

我已经在网络上注册了一个成员,但是当尝试使用成员凭据登录时,出现此错误:

(节点: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/Desktop/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()无法处理的承诺。 (拒绝ID:2) (节点:8931)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零退出代码终止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}`);
 });

我该如何解决?

0 个答案:

没有答案