2016-08-09 1 views
2

ShippoのNodeでのAPIの操作経験が豊富な人であれば、これを理解するのに役立ちます。Shippo:生成されたトランザクションのlabel_url変数を他の関数で使用する

私の最終目標は、角度のあるフォームフロントエンドのボタンを押して、出荷トランザクションを作成し、トランザクションのラベルURLを使用してカスタムPDFを作成することです。生成されたラベルURLをPDFテンプレートにプッシュする以外はすべて動作しています。

まず、Shippo's single click label creation exampleをExpress POSTルートに貼り付けました。それはちょうどうまくいって、最近の取引のリストと最近の取引を見るためにShippoのAPIをpingすることでチェックアウトできるトランザクションを生成しました。私が取得する必要があり

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); 
      } 

    }); 

:私の現在のapp.js七宝トランザクションロジックは次のようになりますので、

はその後、私は、lines 102-111 of this code exampleを追加し、それにinstalabelコードで一般的なエラーメッセージジェネレータを置き換えます先ほど作成し、別の関数で使用されたトランザクションのための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":"[email protected]", 
    "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機能自体の外、この応答からの値を使用するために何ができますか?

読んでいただきありがとうございます。

答えて

4

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":"[email protected]", 
}; 

// 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":"[email protected]", 
    "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); 
    } 
}); 
+0

注入されたパラメータの順序を変更することは、そのことを指摘してくれてありがとうと思います。 – wingmatt

関連する問題