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.
1 Answer 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
iniparsermay be more lenient in its parsing thanJSON.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\"": { ... }, ... }.