这是API返回的一些数据。我需要遍历嵌套结构中包含的数组。例如,在下页的savedMajorIds中:
var pg = require("pg");
var fs = require('fs');
const pool = new pg.Pool({
user: 'smurf',
host: 'localhost',
database: 'mydb',
password: 'smurf',
port: 5432,
})
var filename = 'allCountries.txt';
var fs = require('fs'),
es = require('event-stream');
var lineNr = 0;
var max = 11784251; // Number of line, dirty, to get % of lines inserted
// Connect to Postgresql
pool.connect((err, client, done) => {
if (err) throw err
// Stream file line by line
var s = fs.createReadStream(filename)
.pipe(es.split())
.pipe(es.map(function(e, cb) {
lineNr += 1;
// Each line need to be properly formated
e = e.split("\t"); //TAB split
// The following fields need formating
e[0] = parseInt(e[0]);
e[4] = parseFloat(e[4]);
e[5] = parseFloat(e[5]);
e[14] = parseInt(e[14]);
e[15] = e[15] == '' ? 0 : e[15];
e[16] = parseInt(e[16]);
// Insert into db
pool.query('INSERT INTO geonames.rawdata (geonameid, name, asciiname, alternatenames, latitude, longitude, fclass, fcode, country, cc2, admin1, admin2, admin3, admin4, population, elevation, gtopo30, timezone, moddate) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19);', e, function(err, result) {
cb(err, result); // call the callback
console.log("Line added ", lineNr, (lineNr / max * 100) + "%") // Monitor progress
});
})
.resume()
.on('error', function(err) {
done();
console.log('Error while reading file.', err);
})
.on('end', function() {
done();
console.log('Read entire file.')
})
);
}) // END pool.connect
返回是,所以我很确定它正在寻找正确的东西。但是,当我尝试循环获取值时,它会中断。代码是:
const {Pool} = require("pg");
const {StringStream} = require("scramjet");
const fs = require("fs");
const pool = new Pool(options);
const max = 11784251;
const INSERT_ENTRY = 'INSERT INTO geonames.rawdata (geonameid, name, asciiname, alternatenames, latitude, longitude, fclass, fcode, country, cc2, admin1, admin2, admin3, admin4, population, elevation, gtopo30, timezone, moddate) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19);';
StringStream
.from(fs.createReadStream(filename))
.lines()
.parse(line => {
// Each line need to be properly formated
const entry = line.split("\t"); //TAB split
// The following fields need formating
entry[0] = parseInt(entry[0]);
entry[4] = parseFloat(entry[4]);
entry[5] = parseFloat(entry[5]);
entry[14] = parseInt(entry[14]);
entry[15] = entry[15] == '' ? 0 : entry[15];
entry[16] = parseInt(entry[16]);
return entry;
})
.setOptions({maxParallel: 32})
.each(entry => {
const client = await pool.connect();
try {
await client.query(INSERT_ENTRY, entry)
} catch(err) {
console.log('Error while adding line...', err);
// some more logic could be here?
} finally {
client.release();
}
})
.each(() => !(lineNr++ % 1000) && console.log("Line added ", lineNr, (lineNr / max * 100) + "%"))
.run()
.then(
() => console.log('Read entire file.'),
e => console.log('Error while handling file.', err)
);
错误日志不喜欢arrayLen()部分,但到目前为止,我一直无法使它起作用。
答案 0 :(得分:4)
对于任何偶然发现此问题的人:
(i=1, i < arrayLen(apiprofile.result.savedMajorIds),i=i+1)
需要成为
(i=1; i < arrayLen(apiprofile.result.savedMajorIds); i=i+1)
或
(i=1; i < arrayLen(apiprofile.result.savedMajorIds); i++)
答案 1 :(得分:3)
以下是一些选项,具体取决于您的ColdFusion版本。
if (isArray(apiprofile.result.savedMajorIDs)) {
// For/In Loop on Array - Possibly CF9, Definitely CF10+ (Verify version)
// Note: x will leak unless var'ed inside function.
for ( x IN apiprofile.result.savedMajorIDs ) {
writeoutput( x & "<br>" ) ;
}
// ArrayEach - CF10+ > Note: y will not leak.
ArrayEach(apiprofile.result.savedMajorIDs, function(y){writeoutput(y & "<br>");}) ;
// Member Function .each() - CF11+ > Note: z will not leak.
apiprofile.result.savedMajorIDs.each( function(z){writeoutput(z & "<br>");}) ;
}
https://trycf.com/gist/f6f3e64635e4b72da15521a3d49d485f/acf11?theme=monokai