node.js - mongodb Callback inside for loop in nodejs -


I am trying to fill the mongoDB archive with data from Excel. I think I understand the incredible nature of the callback and therefore localized this area using another function before the database callback. However, I can not follow my error.

Here is the code.

  var http = need ('http'); Var parseXlsx = Required ('Excel'); Var Mongo = expected ('mongoskin'); Var faker = is required ('Fake'); Var moment = expected ('moment'); Var object = requires ('mandode'). Object; Var uristring = process.env.MONGOLAB_URI || Process.env.MONGOHQ_URL || 'MongoDB: // local host: 27017 / loadmongo / data'; Var db = mongo.db (uristring, {Native resident: true}); Db.on ('error', console.error.bind (console, 'connection error:')); Db.once ('open', function callback () {console.log ('connect');}); Http.createServer (function handler (req, res) {res.writeHead (200, {'content-type': 'text / plain'}); res.end ('hello world \ n');}). 1337, '127.0.0.1'); Console.log (running on 'Server http://127.0.0.1:1337/'); ParseXlsx ('userRegistration.xlsx', function (hey, data) {if throw (mistake); console.log (data.length); console.log (data [0] [0]); (For the I = 0; I & lt; = data.length; i ++) {// (function (i) {console.log ('for loop inside'); var t = Math.floor (Math.rendum () * 10 ); Var aCD = moment () .subtract ('day', t) .format ('md dd yyyy'); tmpID = new objectID (); console.log (data [i] [0]); Tmp = {'_ id': data [i] [0], 'last name': data [i] [1], 'email 1': data [i] [2], 'email2': data [i Data 'i.e.' [data]: 'country': data [i] [7], 'stat code': data 'data': ACD, 'location': [']' 9], 'city' data [i] [7], ' Data [i] [4], 'country code': data [i] [8] 'data type': [i] [10], 'street name': data [i] [5], 'street number': data [i] [4] 'Service'ededCurrent': {} 'serviceOfferedCurrent': {} '' type ':' point ',' coordinate ': [data [i] [12], data [i] [11]]}}, , 'ServiceCompleted': {}, 'reviewsGiven': {}, 'reviewesRecieved': {}, 'financialRecords': {}}, (function (tmp) {db.collection ('ErRegistration us'). Insert (tmp, function (error, result) {if (err) {console.log (err); Console.log ('some error')} if (result) console.log ('userRegistration archive concludes') ;}); } (Tmp)); } // for loop}}}; // end of parlslsx   

and here is the output

  Server running on http://127.0.0.1:1337/675 Ernesto C: \ Workspace \ Workspace1 \ loadmongo \ node_modules \ excel \ node_modules \ node-promise \ promise.js: Error 204; ^ Writing error: The undefined property '0' can not be read on Celsius: \ Vertical \ workspace1 \ loadmongo \ hello-world-server.js: 43: 22 in Celsius: \ workspace \ workspace1 \ loadmongo \ node_modules \ excel \ excelParser .js: 156: 3 in Object._onImmediate (C: \ workspace \ workspace1 \ loadmongo \ node_modules \ excel \ node_modules \ node promise \ promise.js: 164: 27) at processImmediate [as _immediateCallback] (timers.js: 336: 15) Oh, sorry I did not know that your 2   

console.log ! Okay, you just have to stop your loop before data.length !
 for  (var i = 0; i & lt; data.length; i ++) {    < / P>    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -