我正在Zapier中构建一个自定义集成,以与Monday.com集成。
应该发生的情况是,用户将插入一个项目名称作为输入。该代码将查找所有项目,并创建项目名称及其ids的字典(从技术上讲,这是一个对象)。此字典应使脚本能够查找项目名称的相应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;
});发布于 2020-10-31 04:01:54
问题是,在填充字典之后(从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语句。
https://stackoverflow.com/questions/64593513
复制相似问题