0

我正在 Zapier 中构建自定义集成以与 Monday.com 集成。

应该发生的是用户将插入项目名称作为输入。该代码将查找所有项目并创建项目名称及其 ID 的字典(技术上讲是一个对象)。该字典应该使脚本能够查找项目名称的相应 project_id。

然后会有一些代码将项目插入该项目。

它目前的工作原理是它能够创建字典,但它似乎无法在字典中查找项目 ID,并吐出一个空结果。

我的代码:

//Create variable based on input from user
var project_name = bundle.inputData.project_id;

var dict = {};

//Look up all projects (id and name) in Monday.com
const options_ids = {
  url: "https://api.monday.com/v2",
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Accept: "application/json",
    Authorization: `Bearer ${bundle.authData.access_token}`,
  },
  params: {},
  body: {
    query: "{  boards (ids:811745658){id name items { id name}}}",
  },
};

// Create dictionary of all project names and ids in Monday.com
var p_ids = z.request(options_ids).then((response) => {
  response.throwForStatus();
  const results = z.JSON.parse(response.content);
  var items = results.data.boards[0]["items"];
  for (i = 0; i < items.length; i++) {
    var key = items[i]["name"];
    var value = items[i]["id"];
    dict[key] = value;
  }
  return dict;
});

//Get the project ID of the project name that was entered as an input for use below
const project_id = p_ids[project_name];

const options = {
  url: "https://api.monday.com/v2",
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Accept: "application/json",
    Authorization: `Bearer ${bundle.authData.access_token}`,
  },
  params: {},
  body: {
    // 'query': '{ boards (ids:811745658) {id name items {subitems {id name}} } }'
    query: "{code to insert item to Monday project based on the project_id}",
  },
};

return z.request(options).then((response) => {
  response.throwForStatus();
  items = response.json;
  const results = response.json;

  return results;
});

4

1 回答 1

0

问题是您的代码在填充字典(以 开头const project_id)后不会等待字典被填充以运行。

使用await将有助于排列它:

// Create variable based on input from user
var project_name = bundle.inputData.project_id;

// Look up all projects (id and name) in Monday.com
const options_ids = {
  url: "https://api.monday.com/v2",
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Accept: "application/json",
    Authorization: `Bearer ${bundle.authData.access_token}`,
  },
  params: {},
  body: {
    query: "{  boards (ids:811745658){id name items { id name}}}",
  },
};

const projectIdResponse = await z.request(options_ids);
projectIdResponse.throwForStatus();

const results = z.JSON.parse(response.content);
var items = results.data.boards[0]["items"];

// Create dictionary of all project names and ids in Monday.com
var dict = {};
for (i = 0; i < items.length; i++) {
  var key = items[i]["name"];
  var value = items[i]["id"];
  dict[key] = value;
}

// Get the project ID of the project name that was entered as an input for use below
const project_id = p_ids[project_name];

const options = {
  url: "https://api.monday.com/v2",
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Accept: "application/json",
    Authorization: `Bearer ${bundle.authData.access_token}`,
  },
  params: {},
  body: {
    // 'query': '{ boards (ids:811745658) {id name items {subitems {id name}} } }'
    query: "{code to insert item to Monday project based on the project_id}",
  },
};

return z.request(options).then((response) => {
  response.throwForStatus();
  items = response.json;
  const results = response.json;

  return results;
});

除了使用运算符之外,我没有更改您的代码await,因此您需要仔细检查它是否一切正常。此外,您需要确保您的perform方法async前面有语句。

于 2020-10-30T20:01:53.500 回答