使用 Asana API 正文参数时在 let 正文中添加 IF 逻辑
Posted
技术标签:
【中文标题】使用 Asana API 正文参数时在 let 正文中添加 IF 逻辑【英文标题】:Adding IF logic within a let body when using Asana API body parameter 【发布时间】:2021-12-21 14:25:22 【问题描述】:我正在使用 Zapier 和 Asana API 在 Zapier 的 javascript(使用 Node 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 语句,但无济于事。
【问题讨论】:
【参考方案1】:数据变量应该只是一个普通的 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_on
和 due_on
。
【讨论】:
谢谢!工作得很好。只需要删除body.data.due_on = dateEndFormatted,
末尾的多余逗号
啊,是的,我的错。从 JSON 复制粘贴并忘记将它们更改为分号。我进行了编辑以防其他人将来使用它。以上是关于使用 Asana API 正文参数时在 let 正文中添加 IF 逻辑的主要内容,如果未能解决你的问题,请参考以下文章
使用 Asana API 更新任务时出现“请求没有匹配的路由”消息
使用 opt_fields 请求时,Asana API 未返回受让人信息
asana tasks api 不适用于 GET /tasks GET /workspaces/workspace_id/tasks 但适用于 GET /projects/project_id