使用 Asana API 正文参数时在 let 正文中添加 IF 逻辑

Adding IF logic within a let body when using Asana API body parameter

我正在利用 Zapier 和 Asana API 在 Zapier 的 Javascript(使用节点 10)代码操作中创建一个任务。下面是我当前的代码,目前按预期工作。

但是,我想对其进行更新,以添加允许我在开始日期和结束日期相同的情况下更改某些数据对象的逻辑。

// this is wrapped in an `async` function
// you can use await throughout the function

// get personName
var personName = inputData.personName;
// set dateStart
const dateStart = inputData.dateStart;
// convert the format of MM/DD/YYYY to YYYY-MM-DD for Asana's use
var dateStartFormatted = new Date(dateStart).toISOString().split('T')[0];
// set dateEnd
const dateEnd = inputData.dateEnd;
// convert the format of MM/DD/YYYY to YYYY-MM-DD for Asana's use
var dateEndFormatted = new Date(dateEnd).toISOString().split('T')[0];
// get dateDuration
var dateDuration = inputData.dateDuration;
// get taskURL from the custom fields above
var theURL = inputData.eventLink;
// encode the URL
var theEventLinkFinal = encodeURI(theURL);
// get dateStartPretty
var dateStartPretty = inputData.dateStartPretty;
// get dateEndPretty
var dateEndPretty = inputData.dateEndPretty;
// get projectSection
var projectSection = inputData.projectSection;

// sets custom dropdown field of Team Member to value of OFF
// sets the start date and end date
// sets the task Name based on person chosen in Google Form
// sets task description (notes) with date range, number of days, and link to Google Calendar event
// sets Asana project to "Timeline" and section of the person chosen in the Google Form
// adds Admin and HR users as followers on the task
let body = {
  "data": {
    "custom_fields": {
      "917734345465519": "917734345465549"
    },
    "start_on": dateStartFormatted,
    "due_on": dateEndFormatted,
    "name": personName + " Off",
    "notes": "Date range: " + dateStartPretty + " – " + dateEndPretty + "\n\nTotal number of days: " + dateDuration + "\n\nCalendar link: " + theEventLinkFinal,
    "memberships": [ 
      {
        "project": "893079009430519",
        "section": projectSection
      }
    ],
    "followers": [
      "670933960116792",
      "7747475052050"
    ],
    "workspace": "301669572129"
  }
};
// create the task based on the body and data
const res = await fetch('https://app.asana.com/api/1.0/tasks', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json', 
    'Authorization': 'Bearer 0/XXXXXXXX'
  },
  body: JSON.stringify(body)
});
const data = await res.json();
output = {data: data};

目标是更新上面的代码,所以:

IF dateStartFormatted === dateEndFormatted ... THEN 换出这两行数据:

"start_on": dateStartFormatted,
"due_on": dateEndFormatted,

并替换为这行数据:

"due_at": dateStartFormatted,

我尝试在 let body 中执行 if/else 语句,但无济于事。

数据变量应该只是一个普通的 javascript 对象,因此您应该能够根据条件动态分配属性:

let body = {
  "data": {
    "custom_fields": {
      "917734345465519": "917734345465549"
    },
    "name": personName + " Off",
    "notes": "Date range: " + dateStartPretty + " – " + dateEndPretty + "\n\nTotal number of days: " + dateDuration + "\n\nCalendar link: " + theEventLinkFinal,
    "memberships": [ 
      {
        "project": "893079009430519",
        "section": projectSection
      }
    ],
    "followers": [
      "670933960116792",
      "7747475052050"
    ],
    "workspace": "301669572129"
  }
};

// New: Check for dates to see what property to set for due dates
if (dateStartFormatted === dateEndFormatted) {
    body.data.due_at = dateStartFormatted;
} else {
    body.data.start_on = dateStartFormatted;
    body.data.due_on = dateEndFormatted;
}

// create the task based on the body and data
const res = await fetch('https://app.asana.com/api/1.0/tasks', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json', 
    'Authorization': 'Bearer 0/XXXXXXXX'
  },
  body: JSON.stringify(body)
});

只需确保不在基础 body 对象中包含 start_ondue_on