0

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.

4

2 回答 2

1

您必须像 API 中的其他异步函数一样实现您的函数!第一步:给函数回调

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

第二步:当异步过程结束时调用回调传递结果(你不需要返回线)

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

第三步:以异步方式使用你的函数

gestionar.gestion(myJSONClient,vector_intervalo, function(result){
    console.log(result);
});
于 2013-06-24T22:11:29.497 回答
0

在异步 JS 中,您无法按照您尝试的方式返回值。调用 gestionar.gestion() 时,您需要从主程序传递一个回调函数(您可以将其添加为第三个参数)。

您的代码示例将不起作用,因为函数 gestion() 在设置 intervalo_final 内容之前立即返回。

像这样的东西:

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

然后在函数内:

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
   }
});

另外,我强烈建议阅读一些异步 javascript 教程,例如http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/

Felix 的 Node.js 指南: http: //nodeguide.com/

于 2013-06-24T22:10:58.803 回答