2

我是 NodeJS 的新手,但是一个非常有经验的程序员。我正在做一个副项目来让我的脚沾上节点。我正在寻找 MongoDB 进行存储。开始我只是想测试在 node.js 中创建一些基本的 CRUD 函数。我在下面包含了我的代码。

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

据我了解,我无法将函数参数传递给“read”以指示接下来要调用哪个函数,因为这是异步调用的,而我定义为 mongojs.read 回调的 lambda 函数将没有父函数参数利用。

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

// 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();
4

1 回答 1

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();
于 2012-08-05T17:43:46.100 回答