0

I have array of array that looks like this

var data=[["snp","3569#maspd"],["kpr","ant#$casd"]];

What I want to do is to write this data to a file according to length of the [1]element.

For example, let's take first element in array (which is also an array)

["snp","3569#maspd"]

[1] element 3569#maspd is 10 characters long= length=10, so data from this array will be written into file "10.txt"

I have written a code for it

var result=cont.split("\n").
                map(function(r){return r.split(",").slice(2,4);//.join(":")
            }) //this creates array of arrays


    result.forEach(function(x){
        var length=x[1].length;
        var str=length.toString();


        var fil=length+".txt";
        var txt=x.join(":");
        fs.exists(fil,function(exist){
            if(exist){
                fs.appendFile(fil,txt,function(err){
                    if(err) console.log(err)
                })
            }
            else{
                fs.writeFile(fil,txt,function(err){
                    if(err) console.log(err)
                })
            }
        })

    }

    )

But it always throws an error "cannot read property length of undefined". When I remove all code and leave only

result.forEach(function(x){
    var length=x[1].length;
    var str=length.toString();
        console.log(str)
}
)

It works, did I overlook an bug here? here is an example value of what result array looks like (with random data)

var result=[[ '62346asd5f5510dda8f6223c557bb0bf0b5',
  'MH))WXlhs\'uOSu.iwJk[n}oz#w>T6L' ],
[ '00d7994173ds265bfe71182154a1143b0', '&Df1' ],
[ '1b6c32941719fcbfc76c6e5428e5f5',
  'Fc/0PG#mHb49&#)V|$Swrr7as_*UnL~Y' ],
[ 'f931c2256eca136c97a9a9af4dcae', '.a=]' ]]
2
  • Please show us an example value for cont that would reproduce what you're describing. Commented Sep 22, 2015 at 21:55
  • added it to question Commented Sep 22, 2015 at 22:07

2 Answers 2

1

You have to check/sanitize your input and make sure that x has 2 item.

A basic way to do this could be changing from

var length=x[1].length;

to something like

var length= x[1] != undefined ? x[1].length : 0;

A better way could be filter you array data to make sure it's safe.

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

Comments

0

I believe the problem is in your result array. You most likely have bad data coming in or a problem with your map function. Run the code below with your sample data and it works fine. You can add a try catch to skip it or log it out so you can see the line causing the problem.

fs = require('fs');

var data=[
            [ '62346asd5f5510dda8f6223c557bb0bf0b5',
              'MH))WXlhs\'uOSu.iwJk[n}oz#w>T6L' ],
            [ '00d7994173ds265bfe71182154a1143b0', 
              '&Df1' ],
            [ '1b6c32941719fcbfc76c6e5428e5f5',
              'Fc/0PG#mHb49&#)V|$Swrr7as_*UnL~Y' ],
            [ 'f931c2256eca136c97a9a9af4dcae', 
              '.a=]' ]
         ];

data.forEach(function(x){
    var length= x[1].length;
    var str=length.toString();


    var fil=length+".txt";
    var txt=x.join(":");
    fs.exists(fil,function(exist){
        if(exist){
            fs.appendFile(fil,txt,function(err){
                if(err) console.log(err)
            })
        }
        else{
            fs.writeFile(fil,txt,function(err){
                if(err) console.log(err)
            })
        }
    })

});

Comments

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.