I'm hoping someone with more experience working with Shippo's API in Node can help me figure this out.
我希望在Node有更多使用Shippo API经验的人能帮助我解决这个问题。
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.
我的最终目标是在一个有棱角的表单前端上按下一个按钮,创建发货事务,并使用事务的标签URL创建一个自定义PDF。除了将生成的标签URL推送到PDF模板之外,一切都正常。
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.
首先,我将Shippo的单次单击标签创建示例粘贴到一个Express POST路径中。它工作得很好,生成了一些事务,我可以通过点击Shippo的API来查看最近的事务列表并查看最近的事务列表来检查这些事务。
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:
然后,我添加了这个代码示例的102-111行,并用它替换了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);
}
});
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:
我需要获取刚刚创建的事务的label_url & tracking_number,并在另一个函数中使用它,但是到目前为止我所尝试的一切似乎都以如下的错误结束:
/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:
我猜这是因为创建一个运输标签的函数没有输出响应Shippo正在发送回来,所以我不能在其他函数中使用它…这是对的,还是我弄错了?作为参考,这是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":""
}
What can I do to use the values from this response outside the shippo.transaction.create function itself?
如何在shippo.transaction之外使用此响应的值。创建函数本身?
Thanks for reading.
感谢你的阅读。
1 个解决方案
#1
4
The parameters you're passing to the callback used in shippo.transaction.create
has its injected params reversed. It should be (err, transaction)
not (transaction, err)
. Alternatively, you can chain on a .then()
and pass a callback that only takes (transaction)
and access any error messages in transaction.messages
.
您传递给shippo.transaction中的回调的参数。create将其注入的params反向。它应该是(额,事务)而不是(事务,额)。或者,您可以在a .then()上进行链接,并传递一个只接收(事务)并访问transaction.messages中的任何错误消息的回调。
Below is a working example with the latest version of the shippo-node-wrapper
at the time of this writing.
下面是本文编写时使用的最新版本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);
}
});
#1
4
The parameters you're passing to the callback used in shippo.transaction.create
has its injected params reversed. It should be (err, transaction)
not (transaction, err)
. Alternatively, you can chain on a .then()
and pass a callback that only takes (transaction)
and access any error messages in transaction.messages
.
您传递给shippo.transaction中的回调的参数。create将其注入的params反向。它应该是(额,事务)而不是(事务,额)。或者,您可以在a .then()上进行链接,并传递一个只接收(事务)并访问transaction.messages中的任何错误消息的回调。
Below is a working example with the latest version of the shippo-node-wrapper
at the time of this writing.
下面是本文编写时使用的最新版本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);
}
});