如何解决Zapier代码模式:整合Monday.com
我正在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",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;
});
解决方法
问题在于,填充字典(从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",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
语句。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。