Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return results from a function (javascript, nodejs)

Tags:

Could anyone help me with this code? I need to return a value form a routeToRoom function:

var sys = require('sys');  function routeToRoom(userId, passw) {     var roomId = 0;     var nStore = require('nstore/lib/nstore').extend(require('nstore/lib/nstore/query')());     var users = nStore.new('data/users.db', function() {          users.find({             user: userId,             pass: passw         }, (function(err, results) {             if (err) {                 roomId = -1;             } else {                 roomId = results.creationix.room;             }         }         ));     });     return roomId; } sys.puts(routeToRoom("alex", "123")); 

But I get always: 0

I guess return roomId; is executed before roomId=results.creationix.room. Could someone help me with this code?

like image 554
profesoralex Avatar asked Aug 31 '11 10:08

profesoralex


People also ask

How do I return a result from a function?

To return a value from a function, you must include a return statement, followed by the value to be returned, before the function's end statement. If you do not include a return statement or if you do not specify a value after the keyword return, the value returned by the function is unpredictable.

Can JavaScript functions return values?

JavaScript functions can return a single value. To return multiple values from a function, you can pack the return values as elements of an array or as properties of an object.

Can I return value from callback function JavaScript?

When a callback function completes execution, it returns any return value that it might have to the DataBlade API, which invoked it.


2 Answers

function routeToRoom(userId, passw, cb) {     var roomId = 0;     var nStore = require('nstore/lib/nstore').extend(require('nstore/lib/nstore/query')());     var users = nStore.new('data/users.db', function() {         users.find({             user: userId,             pass: passw         }, function(err, results) {             if (err) {                 roomId = -1;             } else {                 roomId = results.creationix.room;             }             cb(roomId);         });     }); } routeToRoom("alex", "123", function(id) {     console.log(id);     }); 

You need to use callbacks. That's how asynchronous IO works. Btw sys.puts is deprecated

like image 173
Raynos Avatar answered Dec 02 '22 15:12

Raynos


You are trying to execute an asynchronous function in a synchronous way, which is unfortunately not possible in Javascript.

As you guessed correctly, the roomId=results.... is executed when the loading from the DB completes, which is done asynchronously, so AFTER the resto of your code is completed.

Look at this article, it talks about .insert and not .find, but the idea is the same : http://metaduck.com/01-asynchronous-iteration-patterns.html

like image 44
Simone Gianni Avatar answered Dec 02 '22 16:12

Simone Gianni