2016-03-26 16 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オブジェクトの保存// リターンが第二ステップの後、私は非同期でマングースで保存 をKNWが、私​​はステップ2が呼び出されたくない 前にこの
  2. 取得ApprovalMapSchemaでのfindOneの承認者ID
  3. 更新承認者エントリを持つオブジェクトを投稿します。

    post.save(関数(errは、DOC){ 場合(ERR){}他 { はconsole.log({成功:trueの場合、メッセージ: '成功した作成された新しいポスト'、内容:DOC}) ; } });

    /*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

あなたの質問を理解するのが難しいです。しかし、あなたは、この(あなたが何をしたいのかに基づいて、これを変更する必要がありますが、うまくいけば、それはあなたの非同期動作を処理する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

as per user2263572の提案は、非同期ライブラリに行ってきました。

これは、私は今それを実装する方法である:多く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}); 
      } 
     }); 
    } 
); 

感謝!!乾杯!

関連する問題