简体   繁体   English

使用 Javascript 在“Code by Zapier”操作中使用多个获取请求

[英]Use more than one fetch request in “Code by Zapier” action using Javascript

I'm currently trying to make a "Code by Zapier" action for making API requests to IronWifi in order to create a user and fill it with information/details.我目前正在尝试执行“Zapier 代码”操作,以便向 IronWifi 发出 API 请求,以便创建用户并为其填充信息/详细信息。

Currently I've been able to do this with four "Code by Zapier" actions.目前,我已经能够通过四个“Zapier 代码”操作来做到这一点。 Each action is one fetch request.每个动作都是一个获取请求。

However, I was looking to combine all four API requests into one action.但是,我希望将所有四个 API 请求合并为一个操作。 It seems that when a fetch request is used that a 'callback' must be used right after.似乎当使用提取请求时,必须立即使用“回调”。 And I believe that this forces the action to stop at that moment.我相信这会迫使行动在那一刻停止。

Currently I've tried to write code for creating the user and then adding a first name.目前我已经尝试编写用于创建用户然后添加名字的代码。 The code I have so far is as follows:我到目前为止的代码如下:

var headers = {
  "Authorization": "Bearer 22ef59a2eb2a6939f5bd26bb43ff8b2d4d9b24ab",
  "Content-Type": "application/json"
};

var url = "https://us-east1.ironwifi.com/api/users";

var username = inputData.name;


var data = JSON.stringify({"username":username});

//First fetch request (API hit) for creating a user in IronWifi
var user = fetch(url, {method: "POST", headers: headers, body: data}).then(function(binaryResponse) {
  return binaryResponse.json();
}).then(function(jsonResponse) {
  callback(null, {result: jsonResponse.id});
}).catch(callback);

url = url + "/" + user.result;

var firstName = inputData.first
data = JSON.stringify({"firstname":firstName});

//Second fetch request (API hit) for giving the user a first name
var nameFirst = fetch(url, {method: "PATCH", headers: headers, body: data}).then(function(binaryResponse) {
  return binaryResponse.json();
}).then(function(jsonResponse) {
  callback(null, {result: JSON.stringify(jsonResponse)});
}).catch(callback);

var output = {user: user, firstname: nameFirst};
return output;

It seems though that the second fetch request is never being executed but the first fetch request is being executed.似乎从不执行第二个提取请求,但正在执行第一个提取请求。 Would there be a way to have these fetch requests execute successfully in a sequential order?有没有办法让这些获取请求按顺序成功执行?

The source of your confusion here is that you're using a very old style of existing an asynchronous function.您在这里感到困惑的根源是您正在使用现有异步函数的一种非常旧的风格。 .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . When callback is run, then the function exits and your second requests is never called.运行callback ,该函数将退出,并且永远不会调用您的第二个请求。 Instead, you should use await , a better way to write asynchronous JS code.相反,您应该使用await ,这是编写异步 JS 代码的更好方法。

callback is still mentioned in the Zapier docs, but it also notes that await is available (see here ). Zapier 文档中仍然提到了callback ,但它也指出await可用(请参阅此处)。

Try something like this:尝试这样的事情:

const headers = {
  Authorization: "Bearer 22ef59a2eb2a6939f5bd26bb43ff8b2d4d9b24ab",
  "Content-Type": "application/json",
};
const url = "https://us-east1.ironwifi.com/api/users";

// User request setup
const username = inputData.name;
const userBody = JSON.stringify({ username: username });

// First fetch request (API hit) for creating a user in IronWifi
const userResponse = await fetch(url, {
  method: "POST",
  headers,
  body: userBody,
});
const userData = await userResponse.json();
// do error checking?

// Name request setup
const nameUrl = `${url}/${userData.id.result}`; // double check .id.result

const firstname = inputData.first;
const nameBody = JSON.stringify({ firstname });

// Second fetch request (API hit) for giving the user a first name
const nameResponse = await fetch(url, {
  method: "PATCH",
  headers,
  body: nameBody,
});
const nameData = await nameResponse.json();
// do error checking?

return { user, firstname };

That code probably won't work right out of the box - I have no idea what the actual responses look like.该代码可能无法立即使用 - 我不知道实际响应是什么样的。 But, it should get you moving in the right direction.但是,它应该让你朝着正确的方向前进。


Separately, you should reset your API key ( 22ef59... ) and audit any usage of it.另外,您应该重置您的 API 密钥 ( 22ef59... ) 并审核它的任何使用情况。 If it was a valid token, anyone that read this question will have been able to use it.如果它是一个有效的令牌,任何阅读此问题的人都可以使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM