如何在“Zapier代码”中编写node-fetch(Rest-API)?

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

这里是我写的代码,运行良好,当我用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得到的错误是:

如果你正在做asynchronous(与获取库),你需要使用callback!

请帮我解决它。

Zapier知道,获取是一个asynchronous函数。 您必须使用callback函数而不是输出variables。

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