nodejs使用全局变量

时间:2012-08-05 17:32:50

标签: node.js parameters lambda

我是NodeJS的新手,但是一位经验丰富的程序员。我正在做一个侧面项目,让我的脚湿润节点。我正在寻找MongoDB的存储空间。为了开始,我只想测试在节点中创建一些基本的CRUD函数。我在下面提供了我的代码。

我的问题是关于全局变量'更新'。它在'read'和'update'函数中用于控制下一个调用哪个函数。我想按以下顺序调用它们:创建,读取,更新,读取,删除。

我的理解是我不能将函数参数传递给'read'以指示下一个要调用的函数,因为这是异步调用的,我定义的lambda函数作为我对mongojs.read的回调将不会有父函数要使用的函数参数。

这样做的正确方法是什么?使用全局感觉就像一个黑客。 :)

// app.js
//<user>:<password>@<server>/<database>
var connection = "foo:bar@127.0.0.1/blah";
var collections = ["scores"]
var db = require("mongojs").connect(connection, collections);
//console.log(db);

var updated = false;

function my_create()
{
    console.log("my_create");
    db.leaderboard.save({name: "sunder", score: 42}, function(err, result) {
          if(err || !result ) 
          {
              console.log("Score save failed");
              console.log(err);
          }
          else
          {
              console.log("Score save successful");
              my_read();
          }
    });
}

function my_read()
{
    console.log("my_read");
    // app.js
    db.leaderboard.find({name: "sunder"}, function(err, results) {
        if(err || !results)
        {
            console.log("Score read failed");
            console.log(err);
        }
        else 
        {
            results.forEach(function(score) 
            {
                console.log(score);
            });

            // I am sure using globals is a HACK. Need to figure this out. 
            if(updated)
                my_delete();
            else
                my_update();
        }
    });

}

function my_update()
{
    console.log("my_update");
    db.leaderboard.update({name: "sunder"}, {$set: {score: 1337}}, function(err,     result) {
        if(err || !result) 
        {
            console.log("Score update failed");
        }
        else 
        {
            console.log("Score update successful");

            updated = true;
            my_read();
        }
    });
}

function my_delete()
{
    console.log("my_delete");
    db.leaderboard.remove({name: "sunder"}, function(err, result) {
        if(err || !result) 
        {
            console.log("Score delete failed");
        }
        else 
        {
            console.log("Score delete successful");
            process.exit();
        }
    });
}

// kick off the flow of processes
my_create();

1 个答案:

答案 0 :(得分:0)

如何将下一个要执行的操作传递到my_read()函数中,如下所示:

// app.js
var connection = "foo:bar@127.0.0.1/blah";
var collections = ["scores"]
var db = require("mongojs").connect(connection, collections);

function my_create()
{
    console.log("my_create");
    db.leaderboard.save({name: "sunder", score: 42}, function(err, result) {
          if(err || !result ) 
          {
              console.log("Score save failed");
              console.log(err);
          }
          else
          {
              console.log("Score save successful");
              my_read("update");
          }
    });
}

function my_read(next_action)
{
    console.log("my_read");
    // app.js
    db.leaderboard.find({name: "sunder"}, function(err, results) {
        if(err || !results)
        {
            console.log("Score read failed");
            console.log(err);
        }
        else 
        {
            results.forEach(function(score) 
            {
                console.log(score);
            });

            // I am sure using globals is a HACK. Need to figure this out. 
            if(next_action === "delete")
                my_delete();
            else
                my_update();
        }
    });

}

function my_update()
{
    console.log("my_update");
    db.leaderboard.update({name: "sunder"}, {$set: {score: 1337}}, function(err,     result) {
        if(err || !result) 
        {
            console.log("Score update failed");
        }
        else 
        {
            console.log("Score update successful");

            updated = true;
            my_read("delete");
        }
    });
}

function my_delete()
{
    console.log("my_delete");
    db.leaderboard.remove({name: "sunder"}, function(err, result) {
        if(err || !result) 
        {
            console.log("Score delete failed");
        }
        else 
        {
            console.log("Score delete successful");
            process.exit();
        }
    });
}

// kick off the flow of processes
my_create();