0

I'm trying to fill up mongoDB collection with the data from Excel. I think I understand the asynch nature of callback and therefore have localized the scope using another function before the database callback. However, I'm unable to get past my error.

Here is the code.

var http = require('http');
var parseXlsx = require('excel');
var mongo = require('mongoskin');
var faker = require('faker');
var moment = require('moment');
var ObjectID = require('mongodb').ObjectID;

var uristring =
    process.env.MONGOLAB_URI ||
    process.env.MONGOHQ_URL ||
    'mongodb://localhost:27017/loadmongo/data';


var db = mongo.db(uristring, {native_parser:true});

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
  console.log('connected');


});

http.createServer(function handler(req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

parseXlsx('userRegistration.xlsx', function(err, data) {
    if(err) throw err;


    console.log(data.length);
    console.log(data[0][0]);    

    for(var i = 0; i<= data.length; i++) {

        //(function(i){
        console.log('inside for loop');
        var t = Math.floor(Math.random()*10);
        var aCD = moment().subtract('days', t).format('MM DD YYYY');
        var tmpID = new ObjectID();
        console.log(data[i][0]);
        var tmp = {
                '_id': tmpID,
                'firstName': data[i][0],
                'lastName': data[i][1],
                'email1': data[i][2],
                'email2': data[i][3],
                'accountCreationDate': aCD,
                'location': {
                    'country' : data[i][9],
                    'city' : data[i][6],
                    'state' : data[i][7],
                    'stateCode' : data[i][7],
                    'zipcode' : data[i][10],
                    'streetName' : data[i][5],
                    'streetNumber' : data[i][4],
                    'countryCode' : data[i][8],
                    'longlat': {
                        'type' : "Point",
                        'coordinates': [data[i][12], data[i][11]]
                    }

                },
                'serviceNeededCurrent': {

                },
                'serviceOfferedCurrent': {

                },
                'serviceCompleted': {

                },
                'reviewsGiven': {

                },
                'reviewesRecieved': {

                },
                'financialRecords': {

                }
        };

        (function(tmp){
        db.collection('userRegistration').insert(tmp, function(err, result){
            if (err) {
                console.log(err);
                console.log('some error')

            }

            if (result)
                console.log('userRegistration Collection.. Done');


        }); 

        }(tmp));
    } //end of for loop


    }); // End of parsexlsx

and here is the output

Server running at http://127.0.0.1:1337/
675
Ernesto

C:\workspace\workspace1\loadmongo\node_modules\excel\node_modules\node-promise\promise.js:204
          throw error;
                ^
TypeError: Cannot read property '0' of undefined
    at C:\workspace\workspace1\loadmongo\hello-world-server.js:43:22
    at C:\workspace\workspace1\loadmongo\node_modules\excel\excelParser.js:156:3
    at 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)
3
  • It seems to be just a problem with your parseXlsx method, doesn't it? Nothing to do with Mongo, I suppose... Your line 43 is console.log(data[0][0]);, isn't it? Are you sure of your file path? Commented Jul 25, 2014 at 7:26
  • You think ? but, look at the output 675 is the output for console.log(data.length); and Ernesto is the output of console.log(data[0][0]);. Also, when I removed the whole for loop and the database call, I do not see the error. Commented Jul 25, 2014 at 16:26
  • I also noticed, despite seeing the error, db is all filled up correctly. Still cannot figure out the reason for error. Commented Jul 25, 2014 at 18:44

1 Answer 1

2

Oh sorry I didn't realize that those were the outputs of your 2 console.log! Ok you just have to stop your loop before data.length!

for (var i = 0; i < data.length; i++) {

Arrays indexes range from 0 to <arrayLength>-1 ;)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it worked. However, I'm trying to think why my code did not print the contents of the for loop till the error ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.