Pass data from one js file to other synchronously in NodeJS - javascript

I have a situation where I need to service function in service folder from my controller folder and once I receive the output I need to pass it back to UI.
//controller
var service = require('service');
api.get('/users', function(request, response) {
var name = request.query['name'];
var responseFromService = service.someAPI(name);
response.send(responseFromService).status(200);
});
//Service
exports.callTwitterAPI = function(twitterHandle,callback){
var responseFromTwitterService;
console.log("Calling Twitter API.." + twitterHandle);
someAPI.get('users/show', {screen_name: twitterHandle}, function (err, data, res) {
if (err) {
//loggerError.error('No connection to twitter :', Date.now());
responseFromTwitterService = JSON.stringify(err) + "Unable to connect twitter";
} else if (data.errors) {
responseFromTwitterService ="User Not Found!!"
// loggerInfo.info('No Twitter handle found for :', twitterHandle);
} else {
console.log("here..");
responseFromTwitterService = data;
}
console.log('response : '+ responseFromTwitterService);
return (null,responseFromTwitterService);
});
}
Now, I need to hold execution of
response.send(responseFromService).status(200);
this line until my service returns response, I need to make it synchronous.
Thanks in advance :)

Your service is going to either be synchronous by nature, or asynchronous by nature, and how you handle it will be determined by that.
If the service call is synchronous, then what you've written will work fine. If it's asynchronous, then you'll just need to send your response in its callback, e.g.:
//controller
var service = require('service');
api.get('/users', function(request, response) {
var name = request.query['name'];
var responseFromService = service.someAPI(name, function(err, responseFromService) {
response.send(responseFromService).status(200);
});
});
//Service
exports.someAPI = function(name, callback){
//some calculations
return callback(null, responseFromService);
}
EDIT after your update
Your service is never calling the callback you declared. Note your last line:
return (null, responseFromTwitterService);
Doesn't actually do anything. Instead you want:
return callback(null, responseFromTwitterService);
And then your calling code in the controller can be written as I suggested.

Related

Correct way to callback function after all data have been retrieved from mongoDB in Express?

I'm writing a web application with a message feature.
A Conversation in my app is defined as between 2 distinct users. For example if Adam had just created a profile and sent 1 message to Jane and a 3 messages to Jack. He would have 2 Conversations but 4 Messages total.
In the following code on Express side, I'm attempting to retrieve all the Conversations for a given user in the database.
Once this is completed, send this data to the Angular controller side.
What's the correct way to do this in api.js below, taking into account that JS is asynchronous?
public/javascripts/main_controller.js
var mainApp = angular.module('myApp', ['ngRoute', 'btford.socket-io', 'xeditable']);
...
mainApp.controller('MessagesController', function($scope, $http, userSessionService, socket, focus){
console.log("MessagesController running");
$scope.messageDisplay = '';
$scope.username = userSessionService.getUserSession().username;
$http({
url: '/loadConversations',
// url: '/about',
method: "GET"
})
.success(function(response) {
console.log("success with loadConversations: ", response);
console.log(response[0].data);
});
....
})
routes/api.js:
....
router.route('/loadConversations')
.get(isAuthenticated, function(req, res) {
var result = [];
//Find Conversation with each and all distinct users
for(var i = 0; i < req.user.conversations.length; i++){
Conversation.findOne({'_id': req.user.conversations[i]}, function(err, conversation){
if(err){
console.log(err);
}
if(conversation){
var contactUsername = (conversation.initiatorUsername == req.user.username) ? conversation.responderUsername : conversation.initiatorUsername;
var lastMessage = conversation.messages[conversation.messages.length-1].content;
var dateOfMessage = conversation.messages[conversation.messages.length-1].date;
var resultJSON = {contactUsername: contactUsername,
lastMessage: lastMessage,
dateOfMessage: dateOfMessage};
result.push(resultJSON);
} else {
console.log("conversation not found!");
}
//Below is not working, where should I put res.json(result)?
// if(result.length == req.user.conversations.length){
// res.json(result);
// }
});
}
});
I'm not sure you even asked for this, but I think you should slightly rewrite your controller code.
In this example, a promise is being returned after$http(...). Instead of using .success I would recommend using .then. It's important to understand the difference between the two so I'll try to explain briefly. Here's the .success() method in angular source code:
promise.success = function(fn) {
// ...
promise.then(function(response) {
fn(response.data, response.status, response.headers, config);
});
return promise;
};
and here is their official documentation
.success the same as the .then except success will take the first argument of the successful call result, so you wouldn't have to write out response.data. It's sort of convenient, but if you ever use caching or want to chain other promises off of this it becomes a pain in the ass. Here's my alternative for you.
$http({
url: '/loadConversations',
// url: '/about',
method: "GET"
})
.then(function(response) {
// this is your success callback
console.log("success with loadConversations: ", response);
console.log(response[0].data);
}, function(error) {
// if the service call doesnt return 200, we fire this
console.log(error);
});
This console log will be executed asynchronously in this implementation, and will be easier to maintain than using .success would be.
EDIT: for the routes/api.js, I think you just need to return the results array
if(result.length == req.user.conversations.length){
return result;
}

Meteor HTTP.call undefined on Client-side, working on Server-side

I am currently trying to learn how to do HTTP requests in Meteor. When I run the code, I can properly see the data in the console. However, on the client side all I get is "undefined". I believe I'm running the HTTP.get method synchronously.
.JS file
if (Meteor.isClient) {
Template.test.helpers({
testGET: function(){
var origin = Meteor.call('fetchFromService');
console.log(origin); //-- Displays 'Undefined'
}
});
}
if (Meteor.isServer) {
Meteor.methods({
fetchFromService: function() {
this.unblock();
var url = "https://httpbin.org/get";
var result;
try{
result = HTTP.get( url );
} catch(e) {
result = "false";
}
console.log(result.data.origin); //-- Displays the data properly
return result.data.origin;
}
});
}
It's async, you have to pass a callback to the call function:
var origin = Meteor.call('fetchFromService', function(err, data) {
console.log(data);
});
If you don't pass the callback, origin will be undefined until the request finishes.

Angular with hapi js server jsonp

I have an endpoint defined at /api/profile which accepts post parameters.
var http = require('http');
var serverConfig = require('../server.config.js');
var request = require('request');
module.exports = function(server){
server.route({
method: 'POST',
path: '/api/profile',
handler: getProfileData
});
function getProfileData(request, reply){
var battleTag = request.payload.battleTag;
getProfileDataHttp(battleTag, function(err, data){
if(err){
reply(new Error(err));
}
reply(data);
});
}
function getProfileDataHttp(battleTag, callback){
var key = serverConfig.battleNet.apiKey;
var tag = encodeURIComponent(battleTag);
var url = 'https://eu.api.battle.net/d3/profile/'+ tag + '/?locale=en_GB&callback=JSON_CALLBACK&apikey=' + key;
console.log(url);
request(url,function(error, response, body){
if(error){
callback(err);
}
if(!error && response.statusCode ==200){
callback(null, body);
}
});
}
};
it is calling an api with a json callback, when I am receiving the data it is in format:
JSON_CALLBACK({ json data here})
how can I get this endpoint to return just the json data, I have tried JSON.parse() but it causes errors in the server.
the angular service that calls this endpoint is like below:
function getProfileData(battleTag){
var defer = $q.defer();
var tag = validTag(battleTag);
if(!tag){
defer.reject('Invalid Tag please use format 1[a-z]11[a-z0-9]#4[0-9]');
return defer.promise;
}
$http.post('/api/profile', {
battleTag: battleTag
})
.success(function(data){
if(data.reason){
defer.resolve(data.reason);
}
defer.resolve(data);
})
.error(function(err){
defer.reject(err);
});
return defer.promise;
}
the call would work when using $http.jsonp in angular however I had to create the server to hide the secret key from the client
Your question is a bit confusing. You are talking about JSONP, but you want to fetch the data directly.
The whole point of JSONP is to return the data encapsulated inside a function that you choose. You then simply have to execute it.
If you want the data in a direct way, don't use JSONP. Simply do a "normal" call.
After having a quick look at the Battle.net API, it seems that to get the data directly, you should simply omit the 'callback' parameter in the URL of your request.
Thus, your request URL would looks like that:
var url = 'https://eu.api.battle.net/d3/profile/'+ tag + '/?locale=en_GB&apikey=' + key;

Node JS Asynchronous Database Calls

I am having issues getting node to make a database call without proceeding despite the database function has not returned a value.
Here is the basic http server code:
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-origin': '*' // implementation of CORS
});
response.end("ok");
;
}).listen(8080,'0.0.0.0');
Using the request.on('data') function, I am able to decode JSON from requests and proceed that to make a database call:
request.on('data', function (chunk) {
var json = JSON.parse(chunk);
var id = parseInt(json["id"]);
response.end(callDatabase(id));
});
The database function goes something like this:
function callDatabase(id) {
var result;
var connection = mysql.createConnection(
{
host : '192.168.1.14',
user : 'root',
password : '',
database : 'test'
}
);
connection.connect();
var queryString = 'SELECT name FROM test WHERE id = 1';
connection.query(queryString, function(err, rows, fields) {
if (err) throw err;
for (var i in rows) {
result = rows[i].name;
}
});
connection.end();
return result;
}
}
However under testing, this proves that I am doing it wrong. I am aware that I probably want to be using the node asynchronous module, which I have tired. I have also tried using the waterfall method, as well as parallel and many other tutorials online. I feel that the request.on function should be in parallel, then the database call async, so whilst node is waiting for the response from the database server, it is free to get on with any other requests, leaving the queued time to a minimum.
Please inform me if I have miss-understood any of the concepts of node js.
You are returning result and closing the connection before the query has returned it's value from the db. Place that code inside the callback.
Fixing your code, it should look like this:
function callDatabase(id) {
var result;
var connection = mysql.createConnection(
{
host : '192.168.1.14',
user : 'root',
password : '',
database : 'test'
}
);
connection.connect();
var queryString = 'SELECT name FROM test WHERE id = 1';
connection.query(queryString, function(err, rows, fields) {
if (err) throw err;
for (var i in rows) {
result = rows[i].name;
}
connection.end();
return result;
});
}
Although, this will only solve part of the problem, since now you're still calling response.end(callDatabase(id)); before waiting for a response from the query.
In order to fix this, you need to return some kind of callback.
function callDatabase(id, callback) {
// the method code here...
connection.query(queryString, function(err, rows, fields) {
// code...
// instead of returning the result, invoke the callback!
callback(rows);
});
}
Now you can call it like this :
request.on('data', function (chunk) {
var json = JSON.parse(chunk);
var id = parseInt(json["id"]);
callDatabase(id, function(res) {
response.end(res);
});
});

AngularJS ng-click and callback function

On my web application, there are two kinds of users: guests & logged. The main page loads the same content for each.
My goal :
When a registered user clicks the link, 2 ajax requests ($http) retrieve
the data of another page and load them in a model.
If the user is a guest, another model appears saying that he has to register.
My link :
<h4 ng-click="guestAction($event, showOne($event,card.id));">click me</h4>
GuestAction :
$scope.guestAction = function($event, callbackB) {
$http.get('/guest/is-guest/').success(function(data) {
console.log("isGuest retrieved : " + data);
if (data == 1)
{
alert('guest spotted !');
return false;
}
else
{
alert('user');
console.log(callbackB);
eval('$scope.'+callbackB);
}
});
}
This way, if a guest is spotted, we return false and stop the execution. If it's a regular user, we execute the function showOne. As I want to do 2 asynchronous requests one after the other, I chose to use the callback trick.
The problem is that showOne() is executed directly when ng-click is launched. I tried to pass showOne() as a string, and eval() the string in GuestAction, but the parameters become undefined...
Any idea how to solve this problem? I want to use a generic method which fires a function only if the user is logged.
I would recommend using a service and promises, see this AngularJS $q
You don't have to use a service for $http requests but that is just my preference, it makes your controller a lot cleaner
Here is the service with the promise:
app.factory('myService', function ($http, $q) {
var service = {};
service.guestAction = function () {
var deferred = $q.defer();
$http.get('/guest/is-guest/').success(function(data) {
console.log("isGuest retrieved : " + data);
if (data == 1) {
deferred.resolve(true);
} else {
deferred.resolve(false);
}
}).error(function (data) {
deferred.reject('Error checking server.');
});
return deferred.promise;
};
return service;
});
And then in our controller we would call it something like so:
app.controller('myController', function ($scope, myService) {
$scope.guestAction = function($event, card) {
myService.guestAction().then(function (data) {
if (data) {
alert('guest spotted !');
} else {
alert('user');
// Then run your showOne
// If this is also async I would use another promise
$scope.showOne($event, card.id);
}
}, function (error) {
console.error('ERROR: ' + error);
})
};
});
Now obviously you may have to change things here and there to get it working for your needs but what promises do is allow you to execute code and once the promise is returned then continue, I believe something like this is what you are looking for.
You have to pass functions as parameters without the parenthesis and pass in the parameters separately:
<h4 ng-click="guestAction($event,card.id, showOne);">click me</h4>
and
$scope.guestAction = function($event,id, callbackB) {
$http.get('/guest/is-guest/').success(function(data) {
console.log("isGuest retrieved : " + data);
if (data == 1)
{
alert('guest spotted !');
return false;
}
else
{
alert('user');
callbackB($event,id);
}
});
}

Categories