【问题标题】:Extracting a particular Project ID from Asana Task API via Node.js JSON output通过 Node.js JSON 输出从 Asana Task API 中提取特定项目 ID
【发布时间】:2021-05-16 12:33:08
【问题描述】:

使用Asana Task API,我们可以查看任务所属的项目列表,以及这些项目的 GID 和注释(描述文本)。

期望的结果

这里的整个目标是获取在其 Notes 值中包含 #websiteprojecttemplate 的项目的 GID。我们需要找到该项目的 GID,然后将其输出,以便稍后在 Zapier 操作中使用该 GID。

体式任务 API

使用这个 API URL,我们可以看到返回项目数据的 API 输出(包括 GID 和 Notes)。我相信这是 JSON。 https://app.asana.com/api/1.0/tasks/{id}?opt_fields=projects.notes

例如: https://app.asana.com/api/1.0/tasks/1799885428032109?opt_fields=projects.notes 显示为:


当前代码

理想情况下,Node.js Javascript 代码将能够迭代/搜索/找到带有#websiteprojecttemplate 的代码,然后输出匹配的 GID。在这种情况下,它将是:1199916857565229

这是我目前的 JS 代码:

const res = await fetch('https://app.asana.com/api/1.0/tasks/' + inputData.uniqueID + '?opt_fields=projects.notes', {
    headers: {
        'Authorization': 'Bearer 0/899removedforsecurity24564s'
    }
});
const body = await res.json();
const projects = body.data;

output = { id: projects };

输出类似:


  "id" : {
    "gid" : "1199885428032109",
    "projects" : [ {
      "gid" : "810573962916457",
      "notes" : "CRM project to create custom fields for the CRM task"
    }, {
      "gid" : "881219806802782",
      "notes" : "Helps keep PMs aware of what stage of progress the website projects are at; as well as how many projects each PM has (via saved Asana searches)."
    }, {
      "gid" : "1129624391492919",
      "notes" : "Tracks the stage and progress of converting a lead to a client."
    }, {
      "gid" : "1140671985497468",
      "notes" : "Additional CRM project to create more custom fields for the CRM task"
    }, {
      "gid" : "1199916857565229",
      "notes" : "Created from the #websiteprojecttemplate."
    } ]
  }

但我需要它输出的不是整个身体的数据,而是仅输出 gid 值中的 #websiteprojecttemplatenotes 值。


TL;DR

我们如何更新当前代码,以便它可以遍历gid,并在notes 中找到具有#websiteprojecttemplate 的代码,并输出它的gid 编号?

【问题讨论】:

    标签: javascript node.js json asana asana-api


    【解决方案1】:

    我认为您的问题归结为如何在数组中查找元素。

    const project = projects.projects.find(p => p.notes.includes("#websiteprojecttemplate"));
    if (project) {
      console.log(project.gid);
    } else {
      console.log("no matching project");
    }
    

    【讨论】:

    • 这似乎运作良好。出于好奇,为什么使用p 而不是project?只是更短了吗?如果 API 输出中出现另一个以 p 开头的键怎么办?
    • 这部分p => p.notes.includes("#websiteprojecttemplate")是一个函数,它接受一个参数p并返回一个真/假值。参数名称可以是任何名称,并且与 .find 正在操作的数组中的属性无关。是的,它'
    • 是的,它更短:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 1970-01-01
    相关资源
    最近更新 更多