I'm hoping someone with more experience working with Shippo's API in Node can help me figure this out.
My end goal is to press a button on an angular form front end, have the shipment transaction created, and use the transaction's label URL to create a custom PDF. Everything is working except for pushing the generated label URL to the PDF template.
First, I pasted Shippo's single click label creation example into an Express POST route. It worked just fine, generating transactions that I could check out by pinging Shippo's API for a list of recent transactions and viewing the most recent one.
Then, I added lines 102-111 of this code example and replaced the generic error message generator in the instalabel code with it, so my current app.js Shippo transaction logic looks like this:
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);
}
});
I need to grab the label_url & tracking_number for the transaction that was just created and use it in another function, but everything I've tried so far seems to end up with an error like the following:
/src/app.js:88
if(transaction.object_status == "SUCCESS") {
^
TypeError: Cannot read property 'object_status' of null
I'm guessing this is because the function to create a shipping label isn't exporting the response Shippo is sending back, so I can't use it in other functions... Is that accurate, or am I barking up the wrong tree here? For reference, this is what Shippo's response is supposed to] look like:
{
"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":""
}
What can I do to use the values from this response outside the shippo.transaction.create function itself?
Thanks for reading.