NODE.JS function callback to render page - node.js

I have a function getthem() that checks a mongo db for listings and returns name,streamurl for it.
I pass those as var stream to the renderme that renders the /dashboard page.
My problem is that i get the console.log("END OF FIND:"+JSON.stringify(stream))
to show my test input, but nothing goes to the render.
im using ejs to render. How can i get the result passed to the page ?
router.get('/dashboard', function (req, res) {
var foo = getthem();
function getthem() {
var group = "tesint";
console.log('geting for group : ' + group);
var mdlListings = require('../models/listings.js');
var myresult = "tet";
mdlListings.find({}, "name streamurl", function (err, data) {
if (err) {
console.error(err);
return;
}
if (data === null) {
console.log("No results");
return;
}
var stream = { };
data.forEach(function (streams) {
console.log("Got " + streams.name + " " + streams.streamurl);
stream[streams.name] = streams.streamurl;
// stream += 'name: '+streams.name+'},{streamurl: '+streams.streamurl;
// console.log("stram arry "+stream[streams.name] )
console.log("END OF FIND:"+JSON.stringify(stream))
}, renderme(stream));
// console.log("Result:", votes);
//var myresult = Object.keys(stream).map(function (name) {
// return { name: name, url: stream[name] };
//})
console.log("before return stream "+stream);
return stream;
});
}
function renderme(resa) {
console.log("RESA"+JSON.stringify(resa))
var resa = JSON.stringify(resa);
res.render('dashboard', {
title: 'Dashboard',
user: req.user,
listing: resa
}
)
}

You're passing the result of renderme(stream) as a second argument to forEach(). renderme(stream) is then evaluated immediately before your forEach() callback is called, when stream is still an empty object. My guess is you want something like this:
data.forEach(function (streams) {
console.log("Got " + streams.name + " " + streams.streamurl);
stream[streams.name] = streams.streamurl;
console.log("END OF FIND:"+JSON.stringify(stream))
});
renderme(stream);

Actually i figure that why would i pass the function as i could just do whatever i need to directly in there.
That worked perfectly, thanks for the tip.
data.forEach(function (streams) {
console.log("Got " + streams.name + " " + streams.streamurl);
stream[streams.name] = streams.streamurl;
});
res.render('dashboard', {
title: 'Dashboard',
user: req.user,
listing: data
}
)

Related

Node.js param not being passed to callback function

I am creating a Alexa skill (Lambda function) node.js - And I am having having an issue passing in the "zipcode" const to the getLocation callback function, if i output the zipcode it will work.
The getLocation function it's not returning anything, and I am guessing it's because the zipcode param is not being passed in properly into the function.
There is nothing wrong with the function because if i replace the
var url = "localhost/api.php?zip="+zipcode;
with
var url = "localhost/api.php?zip=41242"; or any zip code it works.
What am i doing wrong?
let zipcode = request.intent.slots.zipcode.value;
getLocation(function(location, err) {
if(err) {
context.fail(err);
} else {
options.speechText += location.distance + " miles away,
You can get there by going to " + location.street_address + " in " + location.city + ", " + location.state;
options.endSession = true;
context.succeed(buildResponse(options));
}
});
function getLocation(callback, zipcode) {
var url = "localhost/api.php?zip="+zipcode;
var req = http.get(url, function(res) {
var body = "";
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
body = body.replace(/\\/g, '');
var location = JSON.parse(body);
callback(location);
});
res.on('error', function(err) {
callback('', err);
});
});
}
In the getLocation() function you should pass the zipcode as the second argument . your code will be something like this:
getLocation(function(location, err) {
*/you code here /*
} , zipcode);

Nodejs/Async: How does callback work in iteratee function for async.map as mentioned in code snippet

Being new to nodejs ans async following is the code that I came across.
app = express();
/*
other express use calls like - app.use(bodyParser.json());
*/
var async = require("async");
var server;
app.post('/callType/call', function(req, res) {
var startTime = Date.now();
server = req.body.server;
//async.map asynchronuously call enrollStep1 for every element in the req.body.nodes array
//HOW DOES THIS WORK??!! - WHERE IS THE CALLBACK DEFINED OR SOURCED FROM???
//******************************************************
async.map(req.body.nodes, function(node, callback) {
someFunc(node.property1,node.property2,callback)
},
//This function is called when every task triggered by async.map has called its callback.
function(err, results) {
var response = {};
if (err) {
response.success = false;
response.error = err;
console.log("ERROR returned: " + JSON.stringify(response));
res.json(response);
} else {
var returnResults = [];
//Results is an array of array - flatten it
var flattenedResults = [].concat.apply([], results);
//then remove duplicates
for (var i = 0; i < flattenedResults.length; i++){
var obj = flattenedResults[i];
var isDup = returnResults.some(function(element) {
return element.tid === obj.tid;
});
if (!isDup) {
returnResults.push(obj);
}
}
response.success = true;
response.results = returnResults;
res.json(response);
}
});
});
function someFunc(property1, property2, callback) {
var url = '/'+callTypes +'/'+ call +'/'+ property1 +'/'+ property2
urClient
.get(server + url)
.header('Content-Type', 'application/json')
.end(
function(response) {
if (response.code !== 200) {
callback("Error " + ". Code: " + response.code + " Response: " + JSON.stringify(response));
} else {
callback("Success " + ". Code: " + response.code + " Response: " + JSON.stringify(response));
}
}
);
}
The iteratee function for async.map has a definition starting function(node, callback) { but the callback function is never assigned. How does the callback work over here.
Isn't it supposed to be assigned somewhere like callback = myCallbackFunction;
The async.map takes 3 arguments, the array/object, the function to map the data and the callback function, so your code should be:
async.map(req.body.nodes, someFunc , function(err, results) {
if (err) return console.log(err);
console.log(results);
});
And your someFunc should be:
function someFunc(item, callback) {
// do something with item
// it's each item in the original array/object
callback('The results');
}
This is a basic example: http://code.runnable.com/UyR-6c2DZZ4SmfSh/async-map-example-for-node-js

Getting Response of a function defined in another file in node.js

I have a socket function defined as
var funcs = require('./funcs');
socket.on(EVENT_ACCEPT_ORDER, function(data, callback)
{
data = JSON.parse(data);
var bookingId = data.bookingId;
var userId = data.userId;
console.log("Accepting booking...." + bookingId);
var query = "UPDATE bookings SET bookingStatus = " + BOOKING_STATUS_ACCEPTED + " WHERE id = " + bookingId + " AND bookingStatus = " + BOOKING_STATUS_IN_QUEUE;
con.query(query, function(err, rows,fields)
{
if(err)
{
console.log("mysql query error");
}
else
{
if(rows.changedRows > 0)
{
var indexOfUser = usersList.indexOf(userId);
if(indexOfUser > -1)
{
userSockets[indexOfUser].emit(EVENT_USER_ORDER_ACCEPTED);
}
callback({"message": "Success","error":false, "booking": funcs.getBooking(con, bookingId)});
}
else
callback({"message": "Success","error":true});
}
});
});
Funcs is defined as
module.exports = {
"getBooking": function (con, bookingId)
{
var query = "SELECT * FROM bookings WHERE id = " + bookingId + " LIMIT 1";
con.query(query, function(err, rows,fields)
{
if(err)
{
console.log("mysql query error");
}
else if (rows.length == 1)
{
var booking = rows[0];
var userId = rows[0]['userId'];
var query = "SELECT id, name, phone, imagePath FROM users WHERE id = " + userId + " LIMIT 1";
con.query(query, function(err, rows,fields)
{
if(err)
{
console.log("mysql query error");
}
else if (rows.length == 1)
{
booking['user'] = rows[0];
return booking;
}
});
}
});
}
}
Everything is running fine except
callback({"message": "Success","error":false, "booking": funcs.getBooking(con, bookingId)});
in this function instead of booking, i am only getting
{"error":false,"message":"Success"}
Why am i not getting the booking function result?
You are not getting the result, because the result of the callback function in con.query is not returned to the caller of getBooking. It is the asynchronous pattern, which you are not processing correctly.
The way it is supposed to work is that the getBooking gets an extra argument: a function to be called when data are available (in an internal asynchronous call to con.query). Such a function is then provided by the caller and in this function you do whatever you want with the data:
funcs.js
"getBooking": function (con, bookingId, callback) {
...
con.query(query, function(err, rows,fields) {
...
// instead of return booking do
callback(err, booking);
...
}
}
main module
// instead of
callback({"message": "Success","error":false, "booking": funcs.getBooking(con, bookingId)});
// do
funcs.getBooking(con, bookingId, function(err, booking) {
callback({"message": "Success","error":false, "booking": booking});
});
I am afraid this is not the only issue in your code, but this should be the first to fix. Read further about processing asynchronous calls in general and specifically in node.js and fix other places in your code correspondingly.

How to return the callback in a variable in node.js with npm synchronize

I'm using nodejs and I want to avoid multiple nested callbacks. How can I use synchonize to do that ?
More precisely how can I return the pubkeysObj from the callback of the request in a variable and use it in the rest of the script ?
var sync = require('synchronize');
var fiber = sync.fiber;
var await = sync.await;
var defer = sync.defer;
try {
fiber(function() {
console.log('before findKeyPair');
var pubkeysObj2 = await( findKeyPair( defer() ) );
console.log('after findKeyPair pubkeysObj2: ' + pubkeysObj2);
console.log('before makeKeyPairs');
var pubkeyArray2 = await( makeKeyPairs( pubkeysObj2, defer() ) );
console.log('after makeKeyPairs pubkeyArray2: ' + pubkeyArray2);
});
} catch(err) {
console.log('err: ' + err);
}
function findKeyPair(){
Keypair.find({}, {pubkey: 1, _id: 0}, function(err, pubkeysObj) { //mongoose db
if (err) res.redirect('/');
console.log('inside findKeyPair pubkeysObj: ' + pubkeysObj);
return pubkeysObj; // ?????????
});
}
function makeKeyPairs(pubkeysObj3){
console.log('inside makeKeyPairs: pubkeysObj3:' + pubkeysObj3);
var pubkeyArray = [];
pubkeyArray = Object.keys(pubkeysObj3).map(function(_) { return pubkeysObj3[_].pubkey; })
return pubkeyArray; // ????
}
The output is:
before findKeyPair
inside findKeyPair pubkeysObj: { pubkey: 'n2eTmd37KTGhRZNJsf9tfVdCG1YejciETu' },{ pubkey: 'n2cBvz74bMGUf35gAdnSksbBnW1m4HfCmg' }
Would you be open to using wait.for?
I rewrote my code using "wait.for" and now I can receive the object KeypairObj from the database and pass it to the next function makeKeyPairs.
Inside this function I can print my array (pubkeyArray) but
I can't get the returned value (pubkeyArray2) and the last line of the function findKeyPair is not executed.
here is the new code:
var wait = require('wait.for');
var Keypair = require('./app/models/Keypair');
try {
// launch a new fiber
wait.launchFiber(findKeyPair, Keypair);
}
catch(err) {
console.log('err' + err);
}
function findKeyPair(Keypair){
var KeypairObj = wait.forMethod(Keypair,'find', {}, {pubkey: 1, _id: 0});
console.log('1: ' + KeypairObj.toString());
var pubkeyArray2 = wait.for(makeKeyPairs, KeypairObj);
console.log('3: pubkeyArray2: ' + pubkeyArray2); // not executed !!!
}
function makeKeyPairs(pubkeysObj3){
pubkeyArray = Object.keys(pubkeysObj3).map(function(_) { return pubkeysObj3[_].pubkey; });
console.log('2: pubkeyArray: ' + pubkeyArray);
}
and the output:
1: { pubkey: 'n2eTmd37KTGhRZNJsf9tfVdCG1YejciETu' },{ pubkey: 'n2cBvz74bMGUf35gAdnSksbBnW1m4HfCmg' }
2: pubkeyArray: n2eTmd37KTGhRZNJsf9tfVdCG1YejciETu,n2cBvz74bMGUf35gAdnSksbBnW1m4HfCmg

Can't set keys in Redis in Node.js

I am trying to use Redis in Node.js. I am doing something wrong... I can't get my head around it.
var redis = require("redis"), client = redis.createClient();
client.on("error", function (err) {
console.log("error event - " + client.host + ":" + client.port + " - " + err);
});
/* first attempt */
console.log('first attempt');
var before = client.get("test");
console.log(before);
if(client.exists("test")) {
console.log("The value exists");
} else {
console.log("The value doesn't exists");
client.set("test","koen");
}
var after = client.get("test");
console.log(after);
/* second attempt */
console.log('--------------');
console.log('second attempt');
var before = client.get("test");
console.log(before);
if(client.exists("test")) {
console.log("The value exists");
} else {
console.log("The value doesn't exists");
client.set("test","koen");
}
var after = client.get("test");
console.log(after);
I am unable to set any keys... The following is displayed in the console:
first attempt
false
The value doesn't exists
false
--------------
second attempt
false
The value doesn't exists
false
client.set() is not synchronous. You need to supply a callback as the last argument, at which point your key will have been set.
Here is an example how to set and get "test":
client.set("test", "koen", function (err, res) {
console.log("set: ", res)
client.get("test", function (err, res) {
console.log("get: ", res);
});
});
Remember that all your calls must be asynchronous.

Resources