【发布时间】:2021-10-02 17:55:18
【问题描述】:
查看代码中的 cmets 以了解实际发生/未发生的情况。基本上在函数中,array.push() 方法似乎不起作用,我无法理解为什么。任何帮助将不胜感激。
var locationArray = new Array;
request(process.env.RESOURCE_SHEET, (error, response, html) => {
if(!error && response.statusCode == 200) {
const $ = cheerio.load(html);
$("h3").each((i, lle) => {
const location = $(lle).text();
if(location.includes("Kansas")) return;
if(location.includes("In Stock")) {
var level = location + " ✅";
} else {
var level = location + " ❌";
}
locationArray.push(level); // This doesn't push to the array
});
}
});
locationArray.push("test") // This pushes to the array
console.log(locationArray) // Output: "test"
【问题讨论】:
-
request异步工作,您应该尝试在请求回调中控制数组。 -
天啊!!!非常感谢,就是这样。
-
@HarshSaini 您将如何访问
request之外的数组?
标签: javascript arrays callback