return 值在函数外不起作用?
return value is not working outside the function?
当我们 console.log(restaurantdata) 在 .on('end') 函数内 return log 但在函数 log 外部无法给出值 return undefined.
var restaurantdata=[];
fs.createReadStream('restaurantsa9126b3.csv')
.pipe(csv())
.on('data', (data) => restaurantdata.push(data))
.on('end', () => {
return restaurantdata;
})
console.log(restaurantdata);
yield this.render('home',{
restaurantdata:restaurantdata,
});
你可以试试这个...
function convert(data) {
data = data.split('\n');
var heading = data[0].split(',');
var arr = [];
var line,words,err;
for(var i=1;i<data.length-1 ;++i){
var obj = {};
line=data[i];
words = line.split(',');
var k =0;
for(var j=0;j<words.length;++j){
if (words[j].indexOf('\"')==-1){
obj[heading[k]] = words[j];
k++;
} else{
err = [];
err.push(words[j].substring(1,words[j].length));
j++;
while (words[j].indexOf('\"')==-1) {
err.push(words[j]); j++;
}
err.push(words[j].substring(0,words[j].length-1));
obj[heading[k]] = err;
k++;
}
}
arr.push(obj);
}
return arr;
}
希望,这可能对您有所帮助...
当我们 console.log(restaurantdata) 在 .on('end') 函数内 return log 但在函数 log 外部无法给出值 return undefined.
var restaurantdata=[];
fs.createReadStream('restaurantsa9126b3.csv')
.pipe(csv())
.on('data', (data) => restaurantdata.push(data))
.on('end', () => {
return restaurantdata;
})
console.log(restaurantdata);
yield this.render('home',{
restaurantdata:restaurantdata,
});
你可以试试这个...
function convert(data) {
data = data.split('\n');
var heading = data[0].split(',');
var arr = [];
var line,words,err;
for(var i=1;i<data.length-1 ;++i){
var obj = {};
line=data[i];
words = line.split(',');
var k =0;
for(var j=0;j<words.length;++j){
if (words[j].indexOf('\"')==-1){
obj[heading[k]] = words[j];
k++;
} else{
err = [];
err.push(words[j].substring(1,words[j].length));
j++;
while (words[j].indexOf('\"')==-1) {
err.push(words[j]); j++;
}
err.push(words[j].substring(0,words[j].length-1));
obj[heading[k]] = err;
k++;
}
}
arr.push(obj);
}
return arr;
}
希望,这可能对您有所帮助...