javascript - Callbacks within Streams on('data') and on('end') and Mongoose.js -
i reading large .csv
file using fast-csv
myfile.js
var count = 0; var stream = fs.createreadstream("name of file"); fcsv(stream) .on('data', function(data) { modelname.find(query, function(err, docs) { console.log('docs', docs); count = count++; }); }) .on('end', function() { console.log('done', count); }) .parse();
the script runs , list of docs
printed out , on('end')
triggered.
how count
value print out number of docs
? prints out 0
.
any suggestions?
you mixed 2 different styles count
variable. see lets @ little example.
var cnt = 0 (var = 0; < 10; i++) { cnt = cnt++ //<-- bug right here }; console.log(cnt)
what happens cnt
on right side being incremented not assigned left cnt
. reason cnt++
post-incremental , increase happpens after line of code has been completed , therefore after assignment left cnt
.
to see full extent @ second example.
var lines = [] lines[0] = 0 (var = 0; < 10; i++) { lines[i+1] = lines[i]++ //<-- bug (again) right here console.log("lines[" + + "] = " + lines[i] + " \t lines["+ (i+1) + "] = " + lines[i+1]) };
here create empty list lines
, fill next array position lines[i+1]
number current position. current position being post-incremented. you can see in output number of previous array position 1
, number of next 0
.
what want write instead of these:
cnt = ++cnt
cnt = cnt + 1
cnt++
cnt += 1
Comments
Post a Comment