3

サブスクリプションというテーブルとクライアントという別のテーブルがあり、更新を行うたびにサブスクリプションを所有するクライアントの性別が必要です。これが私の更新スクリプトです:

    function update(item, user, request) {
    var subscriptionId = item.id;
    var subscriptionActivitiesTable = tables.getTable("SubscriptionActivity");
    var userTable = tables.getTable("User");
    var activityTable = tables.getTable("Activity");
    var userGender = userTable.where({id: item.UserId}).select('Gender').take(1).read();
    console.log(userGender);
    activityTable.where({PlanId:item.PlanId, Difficulty: item.Difficulty}).read({
         success: function(results){
             var startDate = item.StartDate;
             results.forEach(function(activity)
             {
                var testDate = new Date(startDate.getFullYear(),startDate.getMonth(), startDate.getDate());
                testDate.setDate(testDate.getDate() + activity.Sequence + (activity.Week*7));
                subscriptionActivitiesTable.insert({SubscriptionId: subscriptionId, 
                ActivityId: activity.id, ShowDate: new Date(testDate.getFullYear(), 
                    testDate.getMonth(), testDate.getDate()), CreationDate: new Date()});

             })
         }
     });

     var planWeeks = 12;//VER DE DONDE SACAMOS ESTE NUMERO
     var idealWeight = 0;
     if (userGender === "Male")
     {
        idealWeight = (21.7 * Math.pow(parseInt(item.Height)/100,2));    
     }
     else
     {
         idealWeight = (23 * Math.pow(parseInt(item.Height)/100,2));  
     }

     var metabolismoBasal = idealWeight * 0.95 * 24;
     var ADE = 0.1 * metabolismoBasal;
     var activityFactor;
     if (item.Difficulty === "Easy")
     {
         activityFactor = 1.25;
     }
     else if(item.Difficulty === "Medium")
     {
         activityFactor = 1.5;
     }
     else
     {
         activityFactor = 1.75;
     }
     var caloricRequirement = ((metabolismoBasal + ADE)*activityFactor);
     activityTable.where(function(item, caloricRequirement){
         return this.PlanId === item.PlanId && this.Type != "Sport" && 
         this.CaloricRequirementMin <= caloricRequirement && 
         this.CaloricRequirementMax >= caloricRequirement;}, item, caloricRequirement).read({
         success: function(results)
         {
             var startDate = item.StartDate;
             results.forEach(function(activity)
             {
                for (var i=0;i<planWeeks;i++)
                {
                     var testDate = new Date(startDate.getFullYear(),startDate.getMonth(), startDate.getDate());
                     testDate.setDate(testDate.getDate() + activity.Sequence + (i*7));
                     subscriptionActivitiesTable.insert({SubscriptionId: subscriptionId, 
                     ActivityId: activity.id, ShowDate: new Date(testDate.getFullYear(), 
                     testDate.getMonth(), testDate.getDate()), CreationDate: new Date()});
                }
             })
         }
     })
     request.execute();
}

上記のコードを試しましたが、clientGenderが未定義です。ご覧のとおり、私は性別を使用してidealWeightを設定したいと思います。

4

1 に答える 1

3

このread()メソッドは、関数がパラメーターに渡されることを想定してsuccessいます。思ったように、クエリの結果は返されません。

代わりに次のようなものを試してください。

function update(item, user, request) {
    var clientTable = tables.getTable("Client");
    var clientGender = 'DEFAULT';
    clientTable.where({id: item.ClientId}).select('Gender').take(1).read({
        success: function(clients) {
            if (clients.length == 0) {
                console.error('Unable to find client for id ' + item.ClientId);
            } else {
                var client = client[0];
                clientGender = client.Gender;

                // since we're inside the success function, we can continue to 
                // use the clientGender as it will reflect the correct value
                // as retrieved from the database
                console.log('INSIDE: ' + clientGender);
            }
        }
    });

    // this is going to get called while the clientTable query above is
    // still running and will most likely show a value of DEFAULT 
    console.log('OUTSIDE: ' + clientGender);

}

このサンプルでは、​​パラメーターで提供されるコールバック関数を使用して、クライアントテーブルクエリが開始されsuccessます。クエリが終了すると、コールバック関数が呼び出され、結果のデータがログに表示されます。一方、クエリの実行中、つまり// /流暢whereなコードが実行された後の次のステートメントでは、関数外のclientGenderフィールドの値を表示するために別のステートメントが実行されます。このコードは、ステートメントがデータベースで待機している間に実行されます。WAMSログでの出力は次のようになります。takeselectreadconsole.logreadread

* INSIDE: Male
* OUTSIDE: Default

ログの下部に最も古いエントリが表示されているため、OUTSIDEログエントリがINSIDEログの前に書き込まれたことがわかります。

非同期プログラミングや関数型プログラミングに慣れていない場合、これは奇妙に見えるかもしれませんが、私が見つけた限りでは、これはノードが機能するようになりました。関数にネストされた関数にネストされた関数はちょっと怖くなるかもしれませんが、前もって計画していれば、おそらくそれほど悪くはないでしょう:-)

于 2012-11-30T05:36:03.153 に答える