3

我有一个名为 Subscription 的表和另一个名为 Client 的表,我每次进行更新时都需要拥有订阅的客户的性别。这是我的更新脚本:

    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 是未定义的。如您所见,我想使用性别来设置理想体重。

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语句,执行另一条take语句,显示函数外的clientGender字段的值。此代码将在语句仍在数据库上等待时运行。您的输出在 WAMS 日志中应如下所示:selectreadconsole.logreadread

* INSIDE: Male
* OUTSIDE: Default

由于日志在底部显示最旧的条目,因此您可以看到 OUTSIDE 日志条目是在 INSIDE 日志之前的某个时间写入的。

如果您不习惯异步或函数式编程,这可能看起来很奇怪,但据我发现,这现在是节点工作。嵌套在函数中的函数可能会有点吓人,但如果你提前计划,它可能不会太糟糕:-)

于 2012-11-30T05:36:03.153 回答