I'm using Elasticsearch JS client and node to learn ES and Javascript. I'm using Javascript build system in SublimeText2 defined like this to run the JS code:
{
"cmd": ["C:\\Program Files\\nodejs\\node.exe", "$file"],
"selector": "source.js"
}
Wrote this to submit data to ES for indexing:
"use strict";
const es = require('elasticsearch');
const path = require('path');
const fs = require('fs');
function es_connect(url, loglevel) {
if (typeof loglevel === 'undefined') { // somehow default function params feature from ES6 is not available in installable node/js
loglevel == 'error';
}
return new es.Client({host: url, log:loglevel});
}
function get_content(fname) {
const raw = fs.readFileSync(path.join('data', fname));
const content = JSON.parse(raw);
console.log('Found ' + content.objects.length + ' objects.');
return content;
}
function index_json_list(fname, index, doc_type, url) {
var content = get_content(fname);
var client = es_connect(url);
var results = [];
function result_cb(err, resp) {
console.log('Pushing error ' + err + ' and response ');
console.log(resp);
results.push({error:err, response:resp});
};
content.objects.forEach(function(x) {
console.log('___Submitting ');
console.log(x);
client.index({
index: index,
type: doc_type,
body: x
},
result_cb);
});
results.forEach(function(x){
console.log('indexing result: ' + x);
})
console.log('results');
console.log(results);
}
index_json_list('us_presidents.json', 'officials', 'president', 'http://localhost:9200/');
Data source: https://github.com/dariusk/corpora/blob/master/data/humans/us_presidents.json
Output:
Found 66 objects.
___Submitting
{ website: '',
startdate: '2009-01-20',
role_type_label: 'President',
....
leadership_title: null }
results
[]
Pushing error undefined and response
{ _index: 'officials',
_type: 'president',
_id: 'AVhOXERCNHzrCLGOfUu1',
_version: 1,
result: 'created',
_shards: { total: 2, successful: 1, failed: 0 },
created: true }
Pushing error undefined and response
{ _index: 'officials',
_type: 'president',
_id: 'AVhOXERBNHzrCLGOfUu0',
_version: 1,
result: 'created',
_shards: { total: 2, successful: 1, failed: 0 },
created: true }
...
Questions:
It's obvious why printing
results
outputs empty array, but the question is how can I wait for those callbacks to complete? (I don't mean waiting synchronously, but rather in async callback manner). It can probably be done using promises, but I have not learned promises yet and for now want to learn how to do this "callback" way.Is there some way to make string concatenation on JSON object not to get the representation like
[object Object]
but instead use object literal? (if I callconsole.log(obj)
I get string representation of object literal, not this[object Object]
"shorthand"). Using.toString()
is no good.