Node.js代码从网页中提取所有链接并将其存储在变量中,该变量将数据存储在函数范围内,但不显示数据超出函数范围。
var ineed = require('ineed');
var url = require('get-urls');
var list = require('collections/list');
var fs = require('fs');
var arr = [];
ineed.collect.hyperlinks.from("https://energy.economictimes.indiatimes.com/rss/", function (err, response, result) {
var links = url(JSON.stringify(result)).toArray();
var str="/rss/";
for(var i = 0; i<links.length; i++){
if((links[i].search(str))>-1){
arr.push(links[i]);
}
}
console.log(arr);
// I am getting the output of the array here
})
//While printing the array I am not getting the output
console.log(arr);
答案 0 :(得分:1)
您注意到第二个
kind: Service
apiVersion: v1
metadata:
name: gdtapidemo-service
spec:
selector:
app: gdtapidemo-app
ports:
- protocol: TCP
port: 80
targetPort: 9200
只是因为您的收集信息的代码异步运行并且arr在执行第一个console.log
后后接受了任何值。因此,要么您按照
console.log
或转换为异步/等待功能。