在回调之外访问数组
Accessing array outside of a Callback
我正在尝试使用 NodeJS 通过一些基本的网络抓取来构建具有各种键值的对象数组。对于我的代码库的后续步骤,我需要访问父函数之外的 'built' 数组,如您所见,它是:
rp.post(login)
请注意,我使用 express、request-promise 和 cheerio 作为我的主要 npm 依赖项。
let orderArray = []; // Create Empty Array
// Login
rp.post(login, function(err, res, body) {
// Set Cookie Jar
cookieJar = res.headers['set-cookie'];
// Get Order Numbers
rp(getOpenOrders).then(function ($) {
// Check Amount of Open Orders
let openOrderTableLength = $('tbody tr').length;
// Build Out Information
for (let i = 0; i < openOrderTableLength; i++) {
let order = {
vendor: "example vendor",
vendorNum: $('a.show-progress-on-click').eq(i).text(),
vendorNumLink: "https://www.vendor.com." + $('a.show-progress-on-click').eq(i).attr('href'),
status: $('td.w200:nth-of-type(5)').eq(i).text(),
dates: {
ordered: $('td.w100:nth-of-type(3)').eq(i).text(),
eta: "TBA",
shipped: "TBA",
arrived: "TBA",
},
courier: "TBA",
trackingNum: "TBA",
dropShip: false,
items: []
}
if (order.status === "Backorder") {
// Get details from specific
rp({
uri: order.vendorNumLink,
cookie: cookieJar,
transform: body => cheerio.load(body)
})
.then(function ($) {
// Get length of items within
let backOrderItemsTableLength = $('tbody tr').length;
// Build out all the items
for (let j = 0; j < backOrderItemsTableLength; j++) {
let orderItem = {
sku : "TBA",
description: "TBA",
orderQty: "TBA",
allocQty: "TBA",
bOrderQty: "TBA",
eta: $('td:nth-of-type(6)').eq(j).text(),
unitCost: "TBA",
subTotal: "TBA",
}
order.items.push(orderItem);
}
orderArray.push(order);
console.log(orderArray) // Displays a filled array
})
}
}
})
});
console.log(orderArray) // Returns an empty array
正如您将在这段代码的最后一部分中看到的,我已经标记了数组的工作位置以及我需要在父函数之外访问它的位置。
orderArray.push(order);
console.log(orderArray) // Displays a filled array
})
}
}
})
});
console.log(orderArray) // Returns an empty
我要求在内置函数之外访问 'Built' 数组的指针,除此之外,有人可以在适当的时间提供一些信息来完成 return我假设 s、callback() 或 return callback() 可以解决我的问题?
非常感谢!
好的 - 所以我刚刚弄明白了,我仍然没有完全掌握 returns、callback() 或 return callback()[ 的用例=28=],但是,我确实弄清楚了我需要在这里做什么:
首先 - 我将我的代码包裹在一个 命名函数 中,如下所示:
function getInfo (callback){
// Insert all my original code here
};
其次 - 我将 callback(); 添加到构建数组正确记录的部分:
}
orderArray.push(order);
callback();
})
第三 - 我再次使用匿名函数调用了我现在命名的函数:
getInfo (function(){
console.log(orderArray);
});
当我启动我的 NodeJS 应用程序时,它会记录我需要的信息。
我仍然愿意寻求更好地构建我的代码的技巧,但是,这暂时解决了我的问题,以防其他人遇到相同类型的问题。
我正在尝试使用 NodeJS 通过一些基本的网络抓取来构建具有各种键值的对象数组。对于我的代码库的后续步骤,我需要访问父函数之外的 'built' 数组,如您所见,它是:
rp.post(login)
请注意,我使用 express、request-promise 和 cheerio 作为我的主要 npm 依赖项。
let orderArray = []; // Create Empty Array
// Login
rp.post(login, function(err, res, body) {
// Set Cookie Jar
cookieJar = res.headers['set-cookie'];
// Get Order Numbers
rp(getOpenOrders).then(function ($) {
// Check Amount of Open Orders
let openOrderTableLength = $('tbody tr').length;
// Build Out Information
for (let i = 0; i < openOrderTableLength; i++) {
let order = {
vendor: "example vendor",
vendorNum: $('a.show-progress-on-click').eq(i).text(),
vendorNumLink: "https://www.vendor.com." + $('a.show-progress-on-click').eq(i).attr('href'),
status: $('td.w200:nth-of-type(5)').eq(i).text(),
dates: {
ordered: $('td.w100:nth-of-type(3)').eq(i).text(),
eta: "TBA",
shipped: "TBA",
arrived: "TBA",
},
courier: "TBA",
trackingNum: "TBA",
dropShip: false,
items: []
}
if (order.status === "Backorder") {
// Get details from specific
rp({
uri: order.vendorNumLink,
cookie: cookieJar,
transform: body => cheerio.load(body)
})
.then(function ($) {
// Get length of items within
let backOrderItemsTableLength = $('tbody tr').length;
// Build out all the items
for (let j = 0; j < backOrderItemsTableLength; j++) {
let orderItem = {
sku : "TBA",
description: "TBA",
orderQty: "TBA",
allocQty: "TBA",
bOrderQty: "TBA",
eta: $('td:nth-of-type(6)').eq(j).text(),
unitCost: "TBA",
subTotal: "TBA",
}
order.items.push(orderItem);
}
orderArray.push(order);
console.log(orderArray) // Displays a filled array
})
}
}
})
});
console.log(orderArray) // Returns an empty array
正如您将在这段代码的最后一部分中看到的,我已经标记了数组的工作位置以及我需要在父函数之外访问它的位置。
orderArray.push(order);
console.log(orderArray) // Displays a filled array
})
}
}
})
});
console.log(orderArray) // Returns an empty
我要求在内置函数之外访问 'Built' 数组的指针,除此之外,有人可以在适当的时间提供一些信息来完成 return我假设 s、callback() 或 return callback() 可以解决我的问题?
非常感谢!
好的 - 所以我刚刚弄明白了,我仍然没有完全掌握 returns、callback() 或 return callback()[ 的用例=28=],但是,我确实弄清楚了我需要在这里做什么:
首先 - 我将我的代码包裹在一个 命名函数 中,如下所示:
function getInfo (callback){
// Insert all my original code here
};
其次 - 我将 callback(); 添加到构建数组正确记录的部分:
}
orderArray.push(order);
callback();
})
第三 - 我再次使用匿名函数调用了我现在命名的函数:
getInfo (function(){
console.log(orderArray);
});
当我启动我的 NodeJS 应用程序时,它会记录我需要的信息。
我仍然愿意寻求更好地构建我的代码的技巧,但是,这暂时解决了我的问题,以防其他人遇到相同类型的问题。