0

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.

4
  • 1
    Your JSON is invalid. jsonlint.com Commented Jun 1, 2013 at 18:58
  • Related: stackoverflow.com/q/14220321. Not an exact match, but you're experiencing the same problem just from different tasks. Commented Jun 1, 2013 at 19:10
  • @KevinBoucher but when I log that, it returns correctly. or any other suggestion? Commented Jun 1, 2013 at 19:12
  • @rizqy22 iniparser may be more lenient in its parsing than JSON.parse() and similar. But, you should plan for stricter parsing and make sure your JSON is valid. Including: all object keys must be strings and all strings are double-quoted: { "core": { ... }, "remote \"origin\"": { ... }, ... }. Commented Jun 1, 2013 at 19:15

1 Answer 1

1

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

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

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.