我有一個名爲Subscription的表和另一個名爲Client的表,我需要每次進行更新時擁有訂閱的客戶端的性別。這是我的更新腳本:更新不同表格時,如何從特定用戶獲取性別? Azure移動服務
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是未定義的。正如你所看到的,我想用性別來設定理想的重量。
感謝克里斯,我曾經想過,但我想用「clientGender」該函數之外的變量以及使用您所描述的代碼在使用「clientGender」時也會返回undefined。我編輯了問題並粘貼了其他代碼以添加更多上下文。 –
是的,這使得node.js與大多數過程語言不同,因爲它更像是這方面的函數式語言。我認爲你在上面的代碼中遇到的一般問題是,它們都在同一時間執行,而不是按照你喜歡的順序執行。我已經更新了我的答案,以更多地表明我的意思。由於node.js代碼在一種狀態機中執行 - 嵌套函數和回調作爲延續 - 你不能編寫傳統的程序代碼。你必須遵循基於功能/回調的範例... –
感謝Chris,我能夠使用像這樣的超時函數來工作:setTimeout(function(){console.log('OUTSIDE:'+ userGender); },1000);你認爲我應該插入一段代碼在超時內使用性別嗎?還是應該避免? –