[英]How to write node-fetch (Rest-API) in “Code by Zapier”?
在zapier中,我使用Zapier的Code動作。 它基於node.js。 我需要使用訪存來實現我的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得到的錯誤是:
如果您正在執行異步操作(使用抓取庫),則需要使用回調!
請幫助我解決它。
Zapier知道,獲取是一個異步函數。 您必須使用回調函數,而不是輸出變量。
// 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.