gpt4 book ai didi

javascript - 更新不同的表时如何获取特定用户的性别? Azure 移动服务

转载 作者:行者123 更新时间:2023-11-29 10:49:09 25 4
gpt4 key购买 nike

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

    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。

最佳答案

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/select/read 之后的下一个语句code> 流畅的代码运行后,会执行另一个 console.log 语句以显示 read 函数外部的 clientGender 字段的值。此代码将在 read 语句仍在数据库上等待时运行。您的输出在 WAMS 日志中应如下所示:

* INSIDE: Male
* OUTSIDE: Default

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

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

关于javascript - 更新不同的表时如何获取特定用户的性别? Azure 移动服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13634630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com