【问题标题】:How to write node-fetch (Rest-API) in "Code by Zapier"?如何在“Zapier 代码”中编写节点获取(Rest-API)?
【发布时间】:2015-11-10 22:37:21
【问题描述】:

在 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 库),您需要使用 回调!

请帮我解决。

【问题讨论】:

    标签: node.js rest callback fetch zapier


    【解决方案1】:

    Zapier 知道,fetch 是一个异步函数。您必须使用回调函数而不是输出变量。

    // 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);
      });

    【讨论】:

      猜你喜欢
      • 2015-11-10
      • 2018-09-02
      • 2020-03-10
      • 1970-01-01
      • 2020-11-10
      • 2011-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多