Shippo:在其他函数中使用生成的交易的 label_url 变量

Posted

技术标签:

【中文标题】Shippo:在其他函数中使用生成的交易的 label_url 变量【英文标题】:Shippo: Using generated transaction's label_url variable in other functions 【发布时间】:2016-08-09 22:21:13 【问题描述】:

我希望有更多经验的人在 Node 中使用 Shippo 的 API 可以帮助我解决这个问题。

我的最终目标是在一个角形前端按下一个按钮,创建装运交易,并使用交易的标签 URL 创建一个自定义 PDF。除了将生成的标签 URL 推送到 PDF 模板之外,一切正常。

首先,我将Shippo's single click label creation example 粘贴到 Express POST 路由中。它工作得很好,生成交易,我可以通过 ping Shippo 的 API 来查看最近的交易列表并查看最近的交易。

然后,我添加了lines 102-111 of this code example并用它替换了instalabel代码中的通用错误消息生成器,所以我当前的app.js Shippo事务逻辑如下所示:

shippo.transaction.create(
    "shipment": shipment,
    "carrier_account": "xxxxxxxxxxxxxxxxxxxxxx",
    "servicelevel_token": "ups_ground",
    "label_file_type": "PNG"
,function(transaction, err )
            console.log("transaction : %s", JSON.stringify(transaction, null, 4));
            // print label_url and tracking_number
            if(transaction.object_status == "SUCCESS") 
                console.log("Label URL: %s", transaction.label_url);
                console.log("Tracking Number: %s", transaction.tracking_number);
                exports.label_url = transaction.label_url;
                exports.tracking_number = transaction.tracking_number;
            else
                //Deal with an error with the transaction
                console.log("Message: %s", transaction.messages);
            

    );

我需要获取刚刚创建的交易的 label_url 和 tracking_number 并在另一个函数中使用它,但到目前为止我尝试的所有操作似乎都以如下错误结束:

/src/app.js:88
    if(transaction.object_status == "SUCCESS") 
                  ^

TypeError: Cannot read property 'object_status' of null

我猜这是因为创建运输标签的功能没有导出 Shippo 发回的响应,所以我不能在其他功能中使用它......这是准确的,还是我在吠叫这里的树错了吗?作为参考,七宝的回应应该是这样的:


   "object_state":"VALID",
   "object_status":"SUCCESS",
   "object_created":"2014-07-25T02:09:34.422Z",
   "object_updated":"2014-07-25T02:09:34.513Z",
   "object_id":"ef8808606f4241ee848aa5990a09933c",
   "object_owner":"shippotle@goshippo.com",
   "was_test":true,
   "rate":"ee81fab0372e419ab52245c8952ccaeb",
   "tracking_number":"tracking_number_goes_here",
   "tracking_status":null,
   "tracking_url_provider":"",
   "label_url":"label_url_goes_here",
   "commercial_invoice_url": "",
   "messages":[

   ],
   "customs_note":"",
   "submission_note":"",
   "metadata":""

如何在 shippo.transaction.create 函数本身之外使用此响应中的值?

感谢阅读。

【问题讨论】:

【参考方案1】:

您传递给shippo.transaction.create 中使用的回调的参数已反转其注入参数。它应该是 (err, transaction) 而不是 (transaction, err)。或者,您可以链接 .then() 并传递只接受 (transaction) 的回调并访问 transaction.messages 中的任何错误消息。

以下是撰写本文时最新版本的shippo-node-wrapper 的工作示例。

var shippo = require('./lib/shippo')('<API KEY>');

var addressFrom  = 
    "object_purpose":"PURCHASE",
    "name":"Ms Hippo",
    "company":"Shippo",
    "street1":"215 Clayton St.",
    "city":"San Francisco",
    "state":"CA",
    "zip":"94117",
    "country":"US", //iso2 country code
    "phone":"+1 555 341 9393",
    "email":"ms-hippo@goshippo.com",
;

// example address_to object dict
var addressTo = 
    "object_purpose":"PURCHASE",
    "name":"Mr Hippo",
    "company":"Happy Hippo",
    "street1":"965 Mission St",
    "street2":"Suite 425",
    "city":"San Francisco",
    "state":"CA",
    "zip":"94103",
    "country":"US", //iso2 country code
    "phone":"949-123-4567",
    "email":"mrhippo@goshippo.com",
    "metadata" : "Hippo T-Shirt Order #1043"
;

// parcel object dict
var parcel = 
    "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,
    "parcel": parcel,
    "async": false
;

shippo.transaction.create(
    "shipment": shipment,
        "carrier_account": "07280f4f96f34cc8b75e593c4835dc38",
        "servicelevel_token": "usps_priority",
        "label_file_type": "PNG"
, function (err, transaction) 
    console.log("transaction : %s", JSON.stringify(transaction, null, 4));
    // print label_url and tracking_number
    if (transaction.object_status == "SUCCESS") 
        console.log("Label URL: %s", transaction.label_url);
        console.log("Tracking Number: %s", transaction.tracking_number);
     else 
        //Deal with an error with the transaction
        console.log("Message: %s", transaction.messages);
    
);

// OR 

shippo.transaction.create(
    "shipment": shipment,
        "carrier_account": "07280f4f96f34cc8b75e593c4835dc38",
        "servicelevel_token": "usps_priority",
        "label_file_type": "PNG"
).then(function (transaction) 
    console.log("transaction : %s", JSON.stringify(transaction, null, 4));
    // print label_url and tracking_number
    if (transaction.object_status == "SUCCESS") 
        console.log("Label URL: %s", transaction.label_url);
        console.log("Tracking Number: %s", transaction.tracking_number);
     else 
        //Deal with an error with the transaction
        console.log("Message: %s", transaction.messages);
    
);

【讨论】:

切换注入参数的顺序可以解决问题,感谢您指出并提供示例!

以上是关于Shippo:在其他函数中使用生成的交易的 label_url 变量的主要内容,如果未能解决你的问题,请参考以下文章

Shippo API 导入错误

我正在尝试使用 Shippo 获取要在美国境内发送的 USPS 运输标签,但似乎无法显示任何费率

在 Shippo 中创建 Shipment 对象会返回处于 Queued 状态的对象

Shippo - Python 包异步问题

手动安装 Shippo PHP

将哪些数据发布到 shippo webhook?