在zapier中,我使用Zapier编码的一个动作。它是基于node.js的。我需要使用获取来实现我的客户关系管理的REST。
下面是我编写的代码,当我尝试使用VS代码(Zapier之外)时,它运行得很好:
// the code by zapier includes already the require('fetch')
var api_token = "..."; // my api
var deal_name = "Example"; // a string
fetch("https://api.pipedrive.com/v1/deals/find?term="+deal_name+"&api_token=" + api_token)
.then(function(res) {
return res.json();
}).then(function(json) {
var deal_id = json.data[0].id;
console.log("deal_id="+deal_id);
}).catch(function(error) {
console.log("error");
});
output = {id: 1, hello: "world"}; // must include output...我从Zapier那里得到的错误是:
如果您正在执行异步(使用fetch库),则需要使用回调!
请帮我解决这个问题。
发布于 2015-08-18 16:04:00
在Node.js/回调环境中编写代码时,这是一个典型的错误。
您使用的是
console.log,它将打印到控制台,但不会将数据返回给父服务器(在本例中为Zapier)。
下面是一个坏代码和好代码的示例:
// bad code
fetch(url)
.then(function(res) {
return res.json();
}).then(function(json) {
// when i run this in my node repl it works perfect!
// the problem is this doesn't return the data to zapier
// it just prints it to the system output
console.log(json);
});
// good code
fetch(url)
.then(function(res) {
return res.json();
}).then(function(json) {
// but if i swap this to callback, this works perfect in zapier
callback(null, json);
});我希望这能帮到你!
发布于 2021-08-19 07:39:17
现在,您还可以使用异步/等待,正如示例代码块顶部的默认注释所指出的那样:
// this is wrapped in an `async` function
// you can use await throughout the function
const response = await fetch('http://worldclockapi.com/api/json/utc/now')
return await response.json()请参阅docs中的更多示例:https://zapier.com/help/create/code-webhooks/javascript-code-examples-in-zaps#step-2
请注意,自由层有一个1秒超时(特别是如果您使用Promise.all()执行多个获取!)
https://stackoverflow.com/questions/32058596
复制相似问题