rizqyhi
rizqyhi

Reputation: 153

Pushing json to array return undefined

I have a little problem with javascript here. I've been searching but with no luck. So I have the following json object:

{
  core: {
      repositoryformatversion: '0',
      filemode: 'true',
      bare: 'false',
      logallrefupdates: 'true'
  },
  'remote "origin"': {
      fetch: '+refs/heads/*:refs/remotes/origin/*',
      url: 'https://github.com/user/repo.git'
  },
  'branch "master"': {
      remote: 'origin',
      merge: 'refs/heads/master'
  }
}

and here is my script:

var iniparser = require('iniparser');
var result = [];

iniparser.parse('analytics.js/.git/config', function(err,data){
   result.push(data);
});

console.log(result);

it returns []. Actually, I want to push just the url (https://github.com/user/repo.git) by using result.push(data['remote "origin"'].url). when I use console.log(data['remote "origin"'].url) it returns the url correctly.

Thank you.

Upvotes: 0

Views: 338

Answers (1)

dm03514
dm03514

Reputation: 55942

are you using none-iniparser?

if so it looks like parse is asyncronous (i'm judging by the callback, and that there is a parseSync function.

This means that you dont really know when the callback will be invoked. parse is called ,then your program instantly moves to console.log the result, which hasn't changed yet. THEN at some time, whenever parse is finished, the parse callback is invoked

Upvotes: 1

Related Questions