2013-02-05 56 views
1

有人可以在兩個模型添加到數據庫後幫助我關閉數據庫嗎?我試着讀Synchronous Jake Node.js任務

http://howtonode.org/intro-to-jake

Node.js and Jake - How to call system commands synchronously within a task?

和GitHub的自述 https://github.com/mde/jake

但仍似乎無法弄清楚。這裏的自述

清理後的所有任務運行,傑克「完整的」事件 基地「傑克」對象是EventEmitter,並運行所有任務後觸發一個「完整」事件的摘錄。 當任務啓動一個保持節點事件循環運行的進程(例如數據庫連接)時,這有時很有用。如果你知道你要停止正在運行的節點過程中的所有任務都完成之後,你可以設置一個偵聽器「完整」事件,就像這樣:

jake.addListener('complete', function() { 
    process.exit(); 
}); 

因爲它是現在,它的收盤前的連接它甚至打開它。

// db connect 
var db = require('./schema'); 

desc('Seed MongoDB with initial data'); 
task('seed', [], function() { 

    //******* Populate the database 
    var user1 = new db.userModel({ email: '[email protected]', password: 'x', phone: x }); 
    user1.save(function(err) { 
     if(err) { 
      console.log(err); 
     } else { 
      console.log('user: '+user1.email +' saved'); 
     } 
    }); 
    var user2 = new db.userModel({ email: 'x', password: 'x', phone: x }); 
    user2.save(function(err) { 
     if(err) { 
      console.log(err); 
     } else { 
      console.log('user: '+user2.email +' saved'); 
     } 
    }); 

    db.mongoose.connection.close(); 
    console.log('Closed mongodb connection'); 
}); 

編輯: 我能夠完成我打算利用從parseq模塊平行流。

par(
    function() { 
    fs.readFile("file1", this); 
    }, 
function() { 
    fs.readFile("file2", this); 
}, 
function done(err, results) { 
    ... 
} 
); 

EDIT2: 所以我真的很感謝所有幫助。我也看到你是parseq的維護者:)。 Thx!我仍然掙扎着最後一點,當函數5完成時,我的函數掛起而沒有調用done()。我確定我不正確地打電話給'this'。有什麼建議?

seq(
    function f1() { 
     var user = new db.userModel({ email: 'x' 
         , password: 'x' 
         , phone: x }); 
     user.save(this); 
    }, 
    function f2(err, value) { 
     var user = new db.userModel({ email: 'x' 
         , password: 'x' 
         , phone: x }); 
     user.save(this); 
    }, 
    function f3(err, value) { 
     var merchant = new db.merchantModel({ name: 'x' 
         , logourl: 'x' }); 
     merchant.save(this); 
    }, 
    function f4(err, value) { 
     var merchant = new db.merchantModel({ name: 'x' 
         , logourl: 'x' }); 
     merchant.save(this); 
    }, 
    function f5(err, value) { 
     db.merchantModel.findOne({ name: 'x' }, function(err, merchant) { 
      var coupon = new db.couponModel({ merchant_id: merchant.id 
          , name: 'x' 
          , imageurl: 'x' 
          , expiration: Date.UTC(2013,3,15) }); 
          //, expiration: new Date(2013,3,15) }); //alternate date creation method 
      coupon.save(this); 
     }); 
    }, 
    function done(err) { 
     if(err) { 
      console.log(err); 
     } else { 
      console.log('successfully seeded db'); 
     } 
     db.mongoose.connection.close(); 
     console.log('Closed mongodb connection'); 
    } 
); 

回答

1

在兩個保存功能都完成後,您需要調用db.mongoose.connection.close。最簡單的方法是嵌套保存調用(但不是最漂亮的)。

var user1 = new db.userModel({ email: '[email protected]', password: 'x', phone: x }); 
user1.save(function(err) { 
    if(err) { 
     console.log(err); 
    } else { 
     console.log('user: '+user1.email +' saved'); 
    } 
    var user2 = new db.userModel({ email: 'x', password: 'x', phone: x }); 
    user2.save(function(err) { 
     if(err) { 
      console.log(err); 
     } else { 
      console.log('user: '+user2.email +' saved'); 
     } 
     db.mongoose.connection.close(); 
     console.log('Closed mongodb connection'); 
    }); 
}); 

然後,您應該開始查看流控制庫,以使代碼更簡單。使用parseq

同樣的事情:這裏

var seq = require("parseq").seq; 

seq(
    function f1() { 
    var user1 = new db.userModel({ email: '[email protected]', password: 'x', phone: x }); 
    user1.save(this); 
    }, function f2(err, value) { 
    var user2 = new db.userModel({ email: 'x', password: 'x', phone: x }); 
    user2.save(this); 
    }, function done(err) { 
    // check err here 
    console.log('Closed mongodb connection'); 
    } 
); 

選擇性地忽略一些錯誤,是一個功能如何能看起來像:

function f1() { 
    var self = this; 
    var user1 = new db.userModel({ email: '[email protected]', password: 'x', phone: x }); 
    user1.save(function(err) { 
     if (err === SOMETHING_TO_IGNORE) { 
     self(null); 
     else { 
     self(err); 
     } 
    }); 
    } 
+0

謝謝。任何關於流量控制庫的建議?如果您可以修改我的代碼以使用其中一個,則需要額外的信用。 – user1071182

+0

所以這個工作,但是,如果user1驗證失敗,整個函數停止執行。這很好,我可以每次重新創建數據庫,但理想情況下,我希望能夠在代碼中添加user3,並在已創建user1和user2時通過運行腳本添加它。謝謝您的幫助! – user1071182

+0

是的,這是正常的,並與我提供的代碼預期。你需要一些更精細的東西來忽略特定的錯誤。我會用一些指針來更新我的答案。 –