简体   繁体   English

如何在“ Zapier的代码”中编写节点获取(Rest-API)?

[英]How to write node-fetch (Rest-API) in “Code by Zapier”?

In zapier I use an action of Code By Zapier. 在zapier中,我使用Zapier的Code动作。 It's based on node.js. 它基于node.js。 I need to use fetch for implementing REST-API of my CRM. 我需要使用访存来实现我的CRM的REST-API。

Here is the code I wrote, which runs well when I tried it with VS Code (outside Zapier): 这是我编写的代码,当我在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...

The error I got from Zapier is: 我从Zapier得到的错误是:

If you are doing async (with fetch library) you need to use a callback! 如果您正在执行异步操作(使用抓取库),则需要使用回调!

Please help me with solving it. 请帮助我解决它。

Zapier knows, that fetch is a async function. Zapier知道,获取是一个异步函数。 You have to use the callback-function and not the output variable. 您必须使用回调函数,而不是输出变量。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM