2016-03-26 47 views
0

我正在使用Mongoose,Express和NodeJS實現審批系統。Mongoose save()在findOne()之前返回

每篇文章有2組認可。

我已經實現模型如下:

Post Schema: 
var PostSchema = new Schema({ 
UserId: { type: ObjectId, unique: true, default: null }, 
/.. 
.. Object related fields 
../ 
    Approval1: Object, //chidschema objects of approver 
    Approval2: Object 
}); 

Approval Mapping Schema: 
var ApprovalMapSchema = new Schema({ 
    UserId: { type: ObjectId, unique: true, default: null }, 
    Approver1Id: { type: ObjectId, default: null }, 
    Approver2Id: { type: ObjectId, default: null } 
}); 

我想先找到映射在我的功能,然後更新保存在主哨對象。

我需要在保存新帖子之前檢索兩個審批者ID。

請提出一個好方法去解決它。我目前在做:

  1. 保存新的Post對象保存// 返回第2步之後,我KNW 保存在貓鼬在異步,但我不希望第2步中被稱爲 在此之前
  2. 獲取批准者標識符與批准者映射中的findOne
  3. 更新具有批准者條目的帖子對象。

    post.save(函數(ERR,DOC){ 如果(ERR){} 其他 { 的console.log({成功:真味精: '成功創造了新的崗位',內容:文檔}) ; } });

    /*var actId = activation._id, distributorId, ttlId; 
    
    var approvalMap = ApprovalMap.findOne({ userId: myUserId }, function(err, appMap) { 
        if (err) throw err; 
        else{   
         Approver1Id = appMap.Approver1Id; 
         Approver2Id = appMap.Approver2Id; 
        } 
    }); 
    
    // Create child objects after this and update as subdocument 
    

這不是爲我工作。請幫忙!

回答

1

我很難理解你的問題。但是,它聽起來就像你想要做這樣的事情(你將需要此基礎上修改你想要做什麼,但我希望它表明你的處理異步行爲單程):

//This will run a query that returns a document where userId == myUserId 

    ApprovalMap.findOne({ userId: myUserId }, function(err, appMap) { 

     if (err) throw err; 

     //Once the query is complete function(err, appMap) will be called 
     //err will contain an error object if there was an error 
     //appMap will be the data returned by the query. 
     //within this block you can run another query 
     //for example 

     ApprovalMap.findOne({approved:'some variable'},function(err, secondQueryData){ 

      if (err) throw err; 

      //after this query is complete function(err, secondQueryData) will be called 
      //once again the err argument will be an error, and the 

      //secondQueyrData argument will contain your return data 

      //here you can do something with the data 
      //for example, log the results 

      console.log(secondQueryData) 
     }) 
    }); 

,你可以請參閱,您可以在其他查​​詢的回調中嵌套其他查詢或步驟。這將確保事情按正確的順序運行。您可能還想要簽出npm異步庫或q或bluebird等承諾庫。他們也有一些很棒的解決方案來處理節點中的異步行爲。

+0

謝謝@ user2263572 .. ** asyc **做了詭計......這是一個救世主,一直在爲此奮鬥! – andoidbaby

0

根據@ user2263572建議,去了異步庫。

這是怎麼了我現在實現的是:

async.waterfall(
    [ 
     function(callback){ 
      ApprovalMap.findOne({ userId: myUserId }, function(err, appMap) { 
       if (err) throw err; 
       else{   
        //..Create child approval elements and assign Ids from map 
        callback(err, Approver1Child, Approver2Child); 
       } 
      }); 
     }, 
     function(Approver1Child, Approver2Child, callback){ 
      var post = new Post(req.body); 
      post.distApproval = Approver1Child; 
      post.ttlApproval = Approver2Child; 
      post.userId = myUserId; 
      callback(null, post); 
     } 
    ], 
    function(err, post){ 
     //creating new order 
     post.save(function(err, doc) { 
      if (err) { 
      } 
      else 
      { 
       console.log({success: true, msg: 'Successful created new post', content: doc}); 
      } 
     }); 
    } 
); 

非常感謝@ user2263572!乾杯!