繁体   English   中英

UnhandledPromiseRejectionWarning,Express.js和Node.js

[英]UnhandledPromiseRejectionWarning, Express.js and Node.js

我对Node.jsExpress.js完全陌生,并且一直在尝试通过一些示例将Shippo API集成到我的电子商务Web应用程序中,但是我遇到了一些错误,尽管检查了一下我还是无法解决代码几次。

我收到UnhandledPromiseRejectionWarning错误,从我在线阅读的内容中,这意味着我的代码中某处有一个.then()节,其中不包含“捕获”或“要做的事情”,这是请求返回的内容一个错误。 任何帮助将不胜感激。

这是我的代码:

 var express = require('express') var app = express() var http = require('http'); var Raven = require('raven'); var shippo = require('shippo')('ACCESS_TOKEN'); var engines = require('consolidate'); const bodyParser = require('body-parser'); const path = require('path'); app.use(bodyParser.urlencoded({extended: true})); app.use(bodyParser.json()); app.engine('html', engines.mustache); app.set('view engine', 'html'); //app.use(express.static(path.join(_dirname,'/'))); app.get('/', function (req, res) { res.render('Index.html'); }) app.post('/', function (req, res) { var addressFrom = { "object_purpose":"PURCHASE", "name": "SENDER_NAME", "company":"Shippo", "street1":"215 Clayton St.", "city":"San Francisco", "state":"CA", "zip":"94117", "country":"US", //iso2 country code "phone":"+1 555 341 9393", "email":"SENDER_EMAIL", }; // example address_to object dict var addressTo = { "object_purpose":"PURCHASE", "name": req.body.fnames + ' ' + req.body.lnames, "company": req.body.company, "street1":req.body.street, "city":req.body.city, "state":req.body.state, "zip":req.body.zipcode, "country": req.body.country, //iso2 country code "phone":"+1 555 341 9393", "email":"support@goshippo.com", }; // parcel object dict var parcelOne = { "length":"5", "width":"5", "height":"5", "distance_unit":"in", "weight":"2", "mass_unit":"lb" }; var shipment = { "object_purpose": "PURCHASE", "address_from": addressFrom, "address_to": addressTo, "parcels": [parcelOne], "submission_type": "DROPOFF" }; shippo.transaction.create({ "shipment": shipment, "servicelevel_token": "ups_standard", "carrier_account": 'CARRIER_TOKEN', "label_file_type": "PDF" }) .then(function(transaction) { shippo.transaction.list({ "rate": transaction.rate }) .then(function(mpsTransactions) { mpsTransactions.results.forEach(function(mpsTransaction){ if(mpsTransaction.object_status == "SUCCESS") { console.log("Label URL: %s", mpsTransaction.label_url); console.log("Tracking Number: %s", mpsTransaction.tracking_number); console.log("E-Mail: %s", mpsTransaction.object_owner); console.log(mpsTransaction.object_status); res.status(200).send("Label can be found under: " + mpsTransaction.label_url); } else { // hanlde error transactions console.log("Message: %s", mpsTransactions.messages); } }); }) }, function(err) { // Deal with an error console.log("There was an error creating transaction : %s", err.detail); res.send("something happened :O") }); }) app.post('/successp', function (req, res) { var token = req.body.stripeToken; // Using Express // Charge the user's card: var charge = stripe.charges.create({ amount: 1000, currency: "eur", description: "Example charge", source: token, }, function(err, charge) { // asynchronously called }); res.send('Thanks!') }) app.post('/successp', function (req, res) { var token = req.body.stripeToken; // Using Express // Charge the user's card: var charge = stripe.charges.create({ amount: 1000, currency: "eur", description: "Example charge", source: token, }, function(err, charge) { // asynchronously called }); res.send('Thanks!') }) app.listen(3000, function () { console.log('Example app listening on port 3000!') }) 

这是我得到的错误:

在端口3000上监听的示例应用程序! (节点:2378)UnhandledPromiseRejectionWarning(节点:2378)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。 引发此错误的原因可能是抛出了一个没有catch块的异步函数,或者是拒绝了一个.catch()无法处理的承诺。 (拒绝ID:1)(节点:2378)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。 将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。

我也不完全确定某些代码行的用途(再次,我对表示和node.js非常陌生)。 什么是引擎和胡须? 另外,我看到此示例代码使用APP.POST('/ succesp',function(req,res)...)'/ succesp'到底是什么? 我需要创建另一个HTML文件吗? 另外, 开头的app.use(express.statc([ath.join(_dirnam,'/')));;) ”是什么?

你需要使用的格式小心一点then(FN, errorFn)因为如果有内部的错误then ,该errorFn不会赶上它。 最好使用then(fn).catch(errorFn) 这将允许在任何所有的错误then上面向下筛选到最后catch处理。

例如,第一个调用正确捕获了错误,而第二个则没有:

 function fn() { return Promise.resolve("good") } fn() .then(r => { throw ("whoops") }) .catch(err => console.log(err)) //<-- catch works here fn() .then(r => { throw ("whoops") }, err => console.log(err) // this can't catch the error above; it will only catch rejections on fn() ) 

它没有显示在代码段中,但是如果您查看控制台,则会看到未处理的拒绝错误。

在您的代码中,可以通过从shippo.transaction.list返回承诺来shippo.transaction.list承诺链。 然后,您可以在末尾添加一个catch以处理错误。

shippo.transaction.create({
    "shipment": shipment,
    "servicelevel_token": "ups_standard",
    "carrier_account": 'CARRIER_TOKEN',
    "label_file_type": "PDF"
})
.then(function(transaction) {
    return shippo.transaction.list({  // return this promise
    "rate": transaction.rate
})
.then(function(mpsTransactions) {     // so this can flatten out
    mpsTransactions.results.forEach(function(mpsTransaction){
        if(mpsTransaction.object_status == "SUCCESS") {
            console.log("Label URL: %s", mpsTransaction.label_url);
            console.log("Tracking Number: %s", mpsTransaction.tracking_number);
            console.log("E-Mail: %s", mpsTransaction.object_owner);
            console.log(mpsTransaction.object_status);
            res.status(200).send("Label can be found under: " + mpsTransaction.label_url);
        } else {
            // hanlde error transactions
            console.log("Message: %s", mpsTransactions.messages);
        }
    });
})
.catch(function(err) {   // catch errors
    // Deal with an error
    console.log("There was an error creating transaction : %s", err.detail);
    res.send("something happened :O")
});
})

由于很难在没有所有内容的情况下在本地运行,因此我对错误的来源并res.status(200).send() ,但是看起来您正在循环内发送res.status(200).send() ,这可能会导致如果它被调用两次,则会出错。

如果不阅读完整的代码,则在使用Promises时不应尝试使用回调函数捕获错误。 您使用.catch块捕获Promises中的错误

并且您还应该返回第一个承诺,以便将其传递给下一个.then函数(如果您打算将shippo.transaction.list作为mpsTransactions返回)

像这样:

 shippo.transaction.create({
 "shipment": shipment,
 "servicelevel_token": "ups_standard",
 "carrier_account": 'CARRIER_TOKEN',
 "label_file_type": "PDF"
})
  .then(function(transaction) {
      return shippo.transaction.list({
        "rate": transaction.rate
      })
   })
  .then(function(mpsTransactions) {
    mpsTransactions.results.forEach(function(mpsTransaction){
        if(mpsTransaction.object_status == "SUCCESS") {
            console.log("Label URL: %s", mpsTransaction.label_url);
            console.log("Tracking Number: %s", mpsTransaction.tracking_number);
            console.log("E-Mail: %s", mpsTransaction.object_owner);
            console.log(mpsTransaction.object_status);
            res.status(200).send("Label can be found under: " + mpsTransaction.label_url);
        } else {
            // hanlde error transactions
            console.log("Message: %s", mpsTransactions.messages);
        }
    });
   })
  .catch(function (error) {
    // Deal with an error
    console.log("There was an error creating transaction : %s", err.detail);
    res.send("something happened :O")
  });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM