我正在编写一个工具,该工具将监视运送到取件地点的货物。 这基于UPS API和DHL。 我从MS SQL数据库-航空货运单参考中检索了结果集,并希望使用UPS API检查它们是否具有某些状态:'该包裹正在保留中,以备日后交货。 /交货时间将重新安排。 然后我返回带有交付日期和时间的JSON对象,以后再存储回数据库中。
我写了一些函数: -一种用于从数据库中检索AWB的工具 -从UPS检索结果的一个
问题是我无法嵌套它们-UPS未被另一个调用(无错误忽略)。
import {upstrack} from './ups';
import {get} from './sql';
get(function(result) {
result.forEach(function (data) {
console.log(data.AWB) // it outputs the AWB numbers to the console correctly
upstrack(data.AWB) // this is not even executed - no error pops in the console
.then(response => console.log(response))
.catch(error => console.log(error));
})
})
upstrack('1Z17704F0470475280') //calling this from the root of file will result with the answer being output to the console
.then(response => console.log(response))
.catch(error => console.log(error));
有什么想法如何调用upstrack(awb)函数以获取结果吗?
ups.js文件:
var Tracking = require('./lib/tracking');
var tracking = new Tracking('myhash', 'mylogin', 'mypass');
let upstrack = (awb) => {
return new Promise(
(resolve, reject) => {
tracking.setJsonResponse(true);
tracking.useSandbox(false);
tracking.makeRequest({
customerContext: "Customer Data", trackingNumber : awb
}, function(data, err) {
if (err) {
reject(err)
}
if (data) {
var results = data.TrackResponse.Shipment[0].Package[0].Activity
var AWB = data.TrackResponse.Shipment[0].ShipmentIdentificationNumber[0]
results.forEach(element => {
if (element.Status[0].StatusType[0].Description[0] == 'This package is being held for a future delivery date. / Delivery will be rescheduled.')
{
var response = []
response.push({
awb: AWB,
status: element.Status[0].StatusType[0].Description[0],
date: element.Date[0].replace(/(\d{4})(\d{2})(\d{2})/g, '$1-$2-$3'),
time: element.Time[0].replace(/(\d{2})(\d{2})(\d{2})/g, '$1:$2:$3')
})
resolve(response)
}
});
}
});
}
)
}
export { upstrack }
答案 0 :(得分:0)
您在这里不能保证resolve
,也不能保证只使用相同分辨率一次resolve
。 else
为假y时应在if(data)
中解析,并且不应resolve
循环,尤其是当您仍可能构建数组时(实际上并非如此)之所以发生是因为response
的范围)。
如果您不熟悉数组迭代函数,强烈建议您学习它们。它们确实可以帮助您清理代码并帮助您轻松完成许多工作。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype
var Tracking = require("./lib/tracking");
var tracking = new Tracking("myhash", "mylogin", "mypass");
let upstrack = awb => {
return new Promise((resolve, reject) => {
tracking.setJsonResponse(true);
tracking.useSandbox(false);
tracking.makeRequest({
customerContext: "Customer Data",
trackingNumber: awb
},
function(data, err) {
if (err) {
reject(err);
}
if (data) {
var results = data.TrackResponse.Shipment[0].Package[0].Activity;
var AWB = data.TrackResponse.Shipment[0].ShipmentIdentificationNumber[0];
resolve(
results
.filter(
element =>
element.Status[0].StatusType[0].Description[0] ==
"This package is being held for a future delivery date. / Delivery will be rescheduled."
)
.map(element => ({
awb: AWB,
status: element.Status[0].StatusType[0].Description[0],
date: element.Date[0].replace(
/(\d{4})(\d{2})(\d{2})/g,
"$1-$2-$3"
),
time: element.Time[0].replace(
/(\d{2})(\d{2})(\d{2})/g,
"$1:$2:$3"
)
}))
);
} else {
resolve([]);
}
}
);
});
};
export {
upstrack
};
在之前和之后添加日志
console.log(data.AWB) // it outputs the AWB numbers to the console correctly
upstrack(data.AWB) // this is executed but since the promise never resolves...
.then(response => console.log(response)) // ...this never logs...
.catch(error => console.log(error)); // ...and this never logs
console.log(data.AWB) // it outputs the AWB numbers to the console