首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编写“Zapier的代码”?

如何编写“Zapier的代码”?
EN

Stack Overflow用户
提问于 2015-08-17 19:53:52
回答 2查看 5.7K关注 0票数 6

在zapier中,我使用Zapier编码的一个动作。它是基于node.js的。我需要使用获取来实现我的客户关系管理的REST。

下面是我编写的代码,当我尝试使用VS代码(Zapier之外)时,它运行得很好:

代码语言:javascript
复制
// 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库),则需要使用回调!

请帮我解决这个问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-18 16:04:00

在Node.js/回调环境中编写代码时,这是一个典型的错误。

您使用的是console.log,它将打印到控制台,但不会将数据返回给父服务器(在本例中为Zapier)。

下面是一个坏代码和好代码的示例:

代码语言:javascript
复制
// 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);
  });

我希望这能帮到你!

票数 11
EN

Stack Overflow用户

发布于 2021-08-19 07:39:17

现在,您还可以使用异步/等待,正如示例代码块顶部的默认注释所指出的那样:

代码语言:javascript
复制
// 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()执行多个获取!)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32058596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档