Wait for Meteor.call result on Client - javascript

I am new to JavaScript.I am not understanding how to wait for a result of an Meteor.call method.
This is my code
//client/main.js
//Added the callback
Template.hello.events({
'click button'(event, instance) {
// increment the counter when button is clicked
instance.counter.set(instance.counter.get() + 1);
var res = Meteor.call("callMeLater","sanj",function (err,res) {
if (err) {
console.log(err);
} else {
console.log("this is the result main ", res);
}
});
console.log("this is the result ", res);
}
//server/main.js
Meteor.methods({
callMeLater :function (name) {
var callMeLaterSync =Meteor.wrapAsync(callMeLaterAsync);
var result = callMeLaterSync(name);
console.log("this is the test", result);
return result;
}
});
var callMeLaterAsync = function (name,cb) {
setTimeout(function () {
cb && cb (null ,"hey there, "+name);
},2000);
};
On the console, i get
this is the result undefined
this is the result main hey there, sanj
How do i wait for the result of Meteor.call by blocking the execution at the client.
Please help
Thanks

Just put your code into a callback method.
Meteor.call('callMeLater',"sanj", function(err, res){
if (err) {
console.log(err);
} else {
console.log("this is the result ", res);
}
});

Related

NodeJS Async Database fetch server freezing

I have an application running on NodeJS(express + mongoose + jade).
I have a post-route /search (all routes are in a separate module) which should handle fetching data from mongo database and inserting it into jade template(in this case just printing th console):
router.post('/search', function (req,res) {
var componentsArray = null;
function getArray(){
console.log('Initializing...');
componentsArray = dataExchanger.search(req.body.select, req.body.selectType, req.body.searchField);
}
getArray(function () {
console.log('Documents returned.');
console.log('Printing array...');
console.log('Array: ' + componentsArray);
console.log('Array type: ' + typeof (componentsArray));
console.log('Rendering page...');
res.render('search_results');
});
});
Searching and fetching function implemented in a different module dataExchanger:
exports.search = function(select, type, data) {
console.log('Fetching documents...');
componentsModel.find({name: data}, function (err, docs) {
if(!err) {
console.log('Returning documents...');
return docs;
} else {
console.log('Can\'t return documents!');
throw err;
}
});
};
The problem is that when I am using a callback function for getArray(), the server just freezes at the moment of returning docs and stops responding.
What am I doing wrong?
Try to use async/await
router.post('/search', async (req,res) => {
let componentsArray;
try {
componentsArray = await dataExchanger.search(req.body.select, req.body.selectType, req.body.searchField);
} catch(e){
//If error in request and no data.
console.error('Error', e.message);
return res.render('error_message');
}
console.log('Documents returned.');
console.log('Printing array...');
console.log('Array: ' + componentsArray);
console.log('Array type: ' + typeof (componentsArray));
console.log('Rendering page...');
res.render('search_results');
});
And here is your dataExchanger
exports.search = function(select, type, data) {
console.log('Fetching documents...');
return new Promise((resolve, reject) => {
componentsModel.find({name: data}, function (err, docs) {
if(err) return reject(err);
resolve(docs);
});
})
};
Further reading: promises, async/await
router.post('/search', function (req,res) {
var componentsArray = null;
function getArray(cb){
console.log('Initializing...');
componentsArray = dataExchanger.search(req.body.select, req.body.selectType, req.body.searchField);
//Execute the callback
cb();
}
getArray(function () {
console.log('Documents returned.');
console.log('Printing array...');
console.log('Array: ' + componentsArray);
console.log('Array type: ' + typeof (componentsArray));
console.log('Rendering page...');
res.render('search_results');
});
});
Looks like your search method is async as well, so you will need to pass the callback down to that to get the desired result.

Elasticsearch.js - wait for ping to complete, async call

I am playing with elasticsearch.js from the browser. I would like to ping elasticsearch, wait for the request to complete and then return the result of the connection. But right now it is happening asynchronously, and returning undefined even when the connection is ok. I have code like this:
var connectionOK = false;
function createElasticsearchClient(hostAddress) {
var client = new $.es.Client({
hosts: hostAddress
});
return client;
}
function checkElasticsearchConnection(client) {
$.when(pingElasticsearch(client)).done(function () {
return connectionOK;
});
}
function pingElasticsearch(client) {
console.log("ELASTICSEARCH: Trying to ping es");
client.ping({
requestTimeout: 30000,
// undocumented params are appended to the query string
hello: "elasticsearch"
}, function (error) {
if (error) {
console.error('ELASTICSEARCH: Cluster is down!');
connectionOK = false;
console.log("INSIDE: " + connectionOK);
} else {
console.log('ELASTICSEARCH: OK');
connectionOK = true;
console.log("INSIDE: " + connectionOK);
}
});
}
and how it is used:
var esClient = createElasticsearchClient("exampleserver.com:9200");
var esCanConnect = (checkElasticsearchConnection(esClient));
You're mixing asynchronous functions with synchronous functions. You could go with this approach instead:
function createElasticsearchClient(hostAddress, callback) {
var client = new $.es.Client({
hosts: hostAddress
});
return callback(client);
}
function pingElasticsearch(client, callback) {
console.log("ELASTICSEARCH: Trying to ping es");
client.ping({
requestTimeout: 30000,
// undocumented params are appended to the query string
hello: "elasticsearch"
}, function (error) {
if (error) {
return callback('ELASTICSEARCH: Cluster is down!');
} else {
return callback(null);
}
});
}
And then run
createElasticsearchClient("exampleserver.com:9200", function(esClient) {
pingElasticsearch(esClient, function(err) {
if (err) console.log(err);
else {
//Everything is ok
console.log('All good');
}
});
});

returning data from node mssql execute functions

I'm using mssql(Microsoft SQL Server client for Node.js) package from npm.I'm trying to execute a stored procedure residing in my sql server database.Everything works fine.However what I want to do is return the recordsets so that i can export this to be used in other module.Below is what I'm trying to do.
function monthlyIceCreamSalesReport (scope){
var connObj = connConfig();
connObj.conn.connect(function(err){
if(err){
console.log(err);
return;
}
connObj.req.input('Month',4);
connObj.req.input('Year',2016);
connObj.req.execute('<myStoredProcedure>', function(err, recordsets, returnValue){
if(err){
console.log(err);
}
else {
console.log(recordsets[0]); // successfully receiving the value
}
connObj.conn.close();
});
});
console.log('check for recordsets', recordsets[0]); // undefined
return recordsets[0];
}
var sqlServerObj = {
monICSalesReport : monthlyIceCreamSalesReport,
};
module.exports = sqlServerObj;
As shown in the code snippet, since the value of recordsets[0] is undefined, exporting this function is of no use.
You can't return this way in async nature. You can get it by passing the callback function
Try to give a callback function like this
function monthlyIceCreamSalesReport(scope, callback) { // pass a callback to get value
var connObj = connConfig();
connObj.conn.connect(function(err) {
if (err) {
console.log(err);
return;
}
connObj.req.input('Month', 4);
connObj.req.input('Year', 2016);
connObj.req.execute('<myStoredProcedure>', function(err, recordsets, returnValue) {
if (err) {
console.log(err);
} else {
console.log(recordsets[0]);
connObj.conn.close();
return callback(null, recordsets[0]); //return as a callback here and get that value in callback from where you called this function
}
});
});
}
var sqlServerObj = {
monICSalesReport: monthlyIceCreamSalesReport,
};
module.exports = sqlServerObj;
Note: See the comment to understand the changes
recordsets[0] is undefinded, because is defined only in connObj.req.execute function scope. You may do this in this way:
function monthlyIceCreamSalesReport (scope, cb){
var connObj = connConfig();
connObj.conn.connect(function(err){
if(err){
console.log(err);
return cb(Error("Something wrong"));
}
connObj.req.input('Month',4);
connObj.req.input('Year',2016);
connObj.req.execute('<myStoredProcedure>', function(err, recordsets, returnValue){
if(err){
console.log(err);
connObj.conn.close();
return cb(Error("Something wrong"));
}
else {
console.log(recordsets[0]); // successfully receiving the value
connObj.conn.close();
return cb(recordsets[0]);
}
});
});
}
var sqlServerObj = {
monICSalesReport : monthlyIceCreamSalesReport,
};
module.exports = sqlServerObj;

"callback is not a function" Node.Js

I have a project written in NodeJs with mysql, async.waterfall
I have also implemented the async.waterfall to avoid my recent problem about 'callback is not a function'
but the problem still exist.
here is my async.waterfall
async.waterfall([
function (callback) {
hold.getEntry(function(data){
var ref = data.ref;
id = data.id;
var message = data.mess;
json = JSON.parse(message);
return callback(null, {'ref':ref, 'id':id, 'json':json});
});
},
function (dataa, callback) {
if(dataa.ref === null){
callback(null);
}else{
hold.checkPositionCandidate(dataa.ref, dataa.id, dataa.json, function(dataaa){
return callback(null, dataaa);
});
}
},
function(anoData, callback) {
console.log(anoData);
if(anoData === true){
//the err is here
hold.getVoterCount(id, json, function(votercount){
if(votercount == 0){
} else {
console.log('last function');
}
});
} else {
}
}
], function (err, results) {
// When finished execute this
});
and this is my getVotercount function
function getVoterCount (id, callback){
pool.getConnection(function(err, con){
con.query("select total_voters as tv from pollwatcher_view where party_id = ?", [id], function(err, rows){
setTimeout(function(){
//this callback is not a function
callback(null, {'count':rows[0].tv});
console.log(rows);
}, 2000);
});
});
}
I am very close to finish my project but that err makes me frustrate. Please someone help me.
You seem to be calling
hold.getVoterCount(id, json, function(votercount){
if(votercount == 0){
} else {
console.log('last function');
}
});
but your getVoterCount function is defined with only 2 expected parameters. I'd suggest trying to only pass in 2 parameters:
hold.getVoterCount(id, function(votercount){
if(votercount == 0){
} else {
console.log('last function');
}
});

How to pass data from Server to Client in Meteor

I am Learning Meteor and Javascript. I am using an npm package to get meta data of an url on the server side. This works fine. But I get undefined when passing that result back to client. Would appreciate some help.
Here is my code
if (Meteor.isClient) {
Meteor.call('getMetaData', "http://www.bbc.co.uk/news", function (err, data) {
if (err) {
console.log("error", err);
};
console.log("Meta data: " + data); //shows undefined
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
var preview = Meteor.npmRequire('page-previewer');
Meteor.methods({
getMetaData: function (url) {
preview(url, function (err, data) {
if (!err) {
console.log(data); //Works fine
return data;
}
});
}
})
});
}
You need to convert the preview function to an synchronous function,using Future like this, this will make this function wait normal err,data callbacks into a synchronous function.
var Future = Npm.require('fibers/future'),
preview = Meteor.npmRequire('page-previewer');
Meteor.methods({
getMetaData: function(url) {
var f = new Future();
preview(url, function(err, data) {
if (!err) {
return f.return(data);
}
});
return f.wait();
}
});
Now this snippet should work
if (Meteor.isClient) {
Meteor.call('getMetaData', "http://www.bbc.co.uk/news", function (err, data) {
if (err) {
console.log("error", err);
}else{
console.log("Meta data: " + data); //shows undefined
}
});
};
try using else block to get the meta data. here's a solution of a similar problem .
https://forums.meteor.com/t/client-getting-undefined-for-server-method/6129/4?u=faysal
so basically you need to add only one extra line
else{ console.log('metadata '+ data);}

Categories

Resources