Mohammad Shahid Mohammad Shahid - 2 months ago 7
Node.js Question

Nodejs Elasticsearch errors are not showing

I am facing problem with nodejs elasticsearch library. Below code is working and giving result if I remove console.log(hello); But if I add this then no error is coming. I am expecting error message of undefined variable "hello". I need to see error message for debugging purpose. What could be the reason?

var elasticsearch = require('elasticsearch');
var esClient = new elasticsearch.Client({
host: 'http://mywebsite.com',
// log: 'trace'
});


body = {};

esClient.search({
index: 'test',
type: 'data',
body: body
}).then(function (resp) {

console.log(hello);
console.log(resp);
});

Answer

Are you sure that the code reaches inside the then function ? maybe search is throwing a error and you have no catch block ???

try :

esClient.search({...})
        .then(function (resp) {

            console.log(hello);
            console.log(resp);    
        })
        .catch(function(error){
            console.log(error);    
        });

see if you getting any error then ?