2016-10-19 9 views
0

条件の場合にコールバックの結果に基づいてSQLクエリを実行しようとしていますが、コードを書き込めません。では、非同期/コールバックメソッドでこれを行う方法をいくつかの情報を提供してください。このコードをnodejsの非同期/コールバックに書き込む方法は?

app.get('/resell-property', function(req, res) { 
    var data = {} 
    data.unit_price_id = 1; 
    function callback(error, result) { 
     if (result.count == 0) { 
      return hp_property_sell_request.create(data) 
     } else if (result.count > 0) { 
      return hp_unit_price.findAll({ 
       where: { 
        unit_price_id: data.unit_price_id, 
        hp_property_id: data.property_id, 
        hp_unit_details_id: data.unit_details_id 
       } 
      }) 
     } 
    } 


    hp_property_sell_request.findAndCountAll({ 
     where: { 
      unit_price_id: data.unit_price_id 
     } 
    }).then(function (result) { 
     if (result) { 
      callback(null, result); 
     } 
    });  
}); 

どうすればこのコールバックを書くことができますか?あなたの質問は正確に答えることがあまりにも曖昧である

if (result.request_id) { 
    return hp_unit_price.findAll({ 
     where: { 
      unit_price_id:result.unit_price_id, 
      hp_property_id:result.property_id, 
      hp_unit_details_id:result.unit_details_id 
     } 
    }).then(function(result) { 
     if (result.is_resale_unit==0 && result.sold_out==0) { 
      return Sequelize.query('UPDATE hp_unit_price SET resale_unit_status=1 WHERE hp_unit_details_id='+result.unit_details_id+' and hp_property_id='+result.property_id) 
     } 
    }) 
} 
+3

スタックオーバーフローのようなものがアドバイスを提供していない、質問に答えるためのものです探します。あなたの答えを見直し、具体的なものを尋ねてください。また、コードのインデントを修正してください。読みにくいです。 – Soviut

答えて

0

:私はコールバックを処理し、このクエリを実行する結果を返す後

hp_property_sell_request.create(data) ,hp_unit_price.findAll({ 
    where: { 
     unit_price_id: data.unit_price_id, 
     hp_property_id: data.property_id, 
     hp_unit_details_id: data.unit_details_id 
    } 
}) 

。 2つのメソッドを呼び出す必要があると仮定しています。最初のメソッドが完了し、2番目のメソッドで使用可能な最初のメソッドの結果が必要な場合は、後者を呼び出す必要があります。

さて、ソリューション

にコールバックし、約束は非常に異なるアプローチであることに注意してください。コールバックと約束を混ぜ合わせないことは、1つだけに固執することが非常に賢明です。あなたの条件のために が、私は非常にコールバックを使用して callback hell.

避けるために約束をお勧めします、解決策は

app.get('/resell-property', function(req, res) { 
method1(req.body.id,function(err,result){ 
    return method2(result); 
}) 
function method1(input1,callback(err,result)){ 
    try{ 
     var result=st.execute("query"); 
     return callback(null,result) 
    }catch(error){ 
     return callback(error,null); 
    } 
} 
function method2(input2){ 
    return input2; 
} 
}) 
関連する問題