Question

I have a main in nodejs for my program where I need to use my result calculated in a module, but my I don't have the right result.

var myJSONClient = {
    "nombre" : "<nombre_cliente>",
    "intervalo" : [0,0]
    };


var intervalo = gestionar.gestion(myJSONClient,vector_intervalo); 
console.log("intervalo: "+intervalo); //return undefined

And this is the module

var gestion = function(myJSON,vector_intervalo) { 
var dburl = 'localhost/mongoapp';
var collection = ['clientes'];
var db = require('mongojs').connect(dburl, collection );
var intervalo_final;

    function cliente(nombre, intervalo){
        this.nombre = nombre;
        this.intervalo = intervalo; 
    }

    var cliente1 = new cliente(myJSON.nombre,myJSON.intervalo);

    db.clientes.save(cliente1, function(err, saveCliente){
    if (err || !saveCliente) console.log("Client "+cliente1.nombre+" not saved Error: "+err);
    else {
        console.log("Client "+saveCliente.nombre+" saved");
        intervalo_final = calculate(vector_intervalo);

        console.log(intervalo_final); //here I can see the right content of the variable intervalo_final
       }
    });

    setTimeout(function(){
        console.log("pause");
    },3000);
    console.log(intervalo_final); //result not correct

 return intervalo_final;
}

exports.gestion = gestion;

I know that node execute my return without wait the end of my function, for this I can't see the right result, but how can I force my program to wait the end of my function? I tried with the setTimeout function but wasn't the right way.

Was it helpful?

Solution 2

In async JS you can't return a value the way it seems you trying to do. You need to pass a callback function from your main program when calling gestionar.gestion() (you can add it as a third argument).

Your code sample won't work because function gestion() returns immediately, before intervalo_final content is set.

Something like this:

gestionar.gestion(myJSONClient,vector_intervalo, function callback(intervalo) {
    // This is the callback function
    console.log("intervalo: " + intervalo);
});

And then within the function:

var gestion = function(myJSON,vector_intervalo, callback) { 
...
db.clientes.save(cliente1, function(err, saveCliente) {
    if (err || !saveCliente) {
        console.log("Client "+cliente1.nombre+" not saved Error: "+err);
        if (callback) callback(); // execute callback function without arguments
    }
    else {
        console.log("Client "+saveCliente.nombre+" saved");
        intervalo_final = calculate(vector_intervalo);

        console.log(intervalo_final);

        if (callback) callback(intervalo_final); // your callback function will be executed with intervalo_final as argument
   }
});

Also, I highly recommend reading some async javascript tutorial, like http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/

And Felix's Node.js Guide: http://nodeguide.com/

OTHER TIPS

You must implement your function just like the other async functions from the API! First step : give callback to function

var gestion = function(myJSON,vector_intervalo, callback) { 

Second step : when the async process is over call callback passing the result (you don't need the return line)

console.log(intervalo_final); //here I can see...
callback(intervalo_final);

Step three: use your function in an async way

gestionar.gestion(myJSONClient,vector_intervalo, function(result){
    console.log(result);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top