如何在“Zapier的代码”中编写提取?

时间:2015-08-17 19:53:53

标签: node.js callback fetch zapier

在zapier中,我使用Code By Zapier的动作。它基于node.js. 我需要使用fetch来实现我的CRM的REST-API。

以下是我编写的代码,当我使用VS Code(在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库),则需要使用a   回调!

请帮我解决。

1 个答案:

答案 0 :(得分:3)

在Node.js / callback环境中编码时,这是一个经典的错误。

  

您正在使用TextView打印到您的控制台,但不会将数据返回到父级(在本例中为Zapier)。

以下是错误和良好代码的示例:

console.log

我希望这有帮助!