Vikaton Vikaton - 1 month ago 11
Javascript Question

Why am I getting a Segmentation fault in Node.js?

I have this function right here:

function createInsta(email, userid) {
if (!fs.existsSync('users/' + email + '/' + userid)) {
fs.mkdir('users/' + email + '/' + userid)
fs.writeFileSync(('users/' + email + '/' + userid + '/instagram.json'), fs.readFileSync("data/data.json", "utf-8"))
console.log("insta folder created");
}
console.log("Initializing Data")
var data = fs.readFileSync('users/' + email + '/' + userid + '/instagram.json', "utf-8")
data = JSON.parse(data);
var result;
request(("https://instagram.com/" + userid + "/?__a=1"), function(error, response, body) {
var res = JSON.parse(body);
result = res.user.followed_by.count
if (error) {
console.log(err)
}
})
while (result === undefined) {
deasync.sleep(100)
}
data.STARTING_COUNTS[0].DAY = result;
data.STARTING_COUNTS[0].WEEK = result;
data.STARTING_COUNTS[0].MONTH = result;

}


right after the console prints outs
Initializing Data
, the console says
Segmentation fault


I have no clue why this is, my code does not seem like anything could cause such an error.

Any pointers? Thanks

Answer

As promised (pardon the pun), here is getting started into converting your code into Promise / await.

This is kind of a start of making your code into using promises, there is more error checking needed etc, but I've deliberately done as little to the code for now too show changes, and we can refactor as we go on.

eg. all that fs.existsSync etc, want's making into promises, so we can get rid of all the sync stuff. Your node.js App will love you for it.

Later, we can do more. Promise's built into browser/node are fine, but I also find a Promise lib can make things easier, I would suggest bluebird -> http://bluebirdjs.com/docs/getting-started.html It might be worth having a good read there too, bluebird has some useful utility functions like promisify that will make your fs.func's easier.

So I think this should do for now, and we'll do another refactor later.

//lets mark this function as async..
async function createInsta(email, userid) {
  return new Promise(function (resolve, reject) {
    if (!fs.existsSync('users/' + email + '/' + userid)) {
      fs.mkdir('users/' + email + '/' + userid)
      fs.writeFileSync(('users/' + email + '/' + userid +  '/instagram.json'), fs.readFileSync("data/data.json", "utf-8"))
      console.log("insta folder created");
    }
    console.log("Initializing Data")
    var data = fs.readFileSync('users/' + email + '/' + userid +  '/instagram.json', "utf-8")
    data = JSON.parse(data);    
    request(("https://instagram.com/" + userid + "/?__a=1"), function(error, response, body) {
      if (error) {
        console.log(err)
        return reject(error);
      }
      var res = JSON.parse(body);
      var result = res.user.followed_by.count
      data.STARTING_COUNTS[0].DAY = result;
      data.STARTING_COUNTS[0].WEEK = result;
      data.STARTING_COUNTS[0].MONTH = result;
      resolve(data); //i'm assuming data is what your wanting to return
    })
  });
}

//to be able to use await our function also needs to be async
async function run() {
 var data = await createInsta('bob@bob.com');
}

Comments